You are on page 1of 28

L p trnh trn mi tr ng Windows

Ngn ng C# - Phn 1

Trn Duy Hong tdhoang@hcmus.edu.vn

.Net Framework

4 ngn ng : C#, VB.NET, C++.NET, Jscript.NET CLR - Common Language Runtime FCL Framework Class Library MSIL Microsoft Intermediate Language

B1 : dch m ngun MSIL

B2 : MSIL m CLR thc thi

ng dng c ngn ng khc vi th vin s dng

.NET Framework

Mi tr ng pht trin

Ngun m Sharp Develop

Pht trin ng dng


Microsoft Visual Studio .NET 2003 / 2005 / 2008 Console Dll Windows form Web

Vng tn - Namespaces
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using MyLibrary;

Vng tn - Namespaces

Namespace l 1 nhm cc lp c mi lin quan

Namespace cha lp v namespace con khc

Vd : System.IO, System.Collection, System.Data Vd : System.Data cha Oledb, SqlClient,

Namespace System cha cha nhiu lp giao tip vi h thng v cc lp cng dng chung

Vng tn - Namespaces

Ton t chm .

Ch nh tn lp trong namespace

Truy xut d liu phng thc ca lp


int x = System.Convert.ToInt32(123);

System.Data.OleDb.OleDbConnection con = new ;

T kha using

Khai bo namespace ta ang s dng


using System.Data.OleDb; OleDbConnection con = new ;

Kiu d liu

Mt s kiu nh sn thng dng


Kiu .NET Boolean Byte Char Single Double Double Kch thc 1 1 2 4 8 8 M t gi tr true hay false Khng du (0..255) M k t Unicode S thc ( 1.5*10-45 .. 3.4*1038) S thc ( 5.0*10-324 .. 1.7*10308) S thc ( 5.0*10-324 .. 1.7*10308)

Kiu bool byte char float double double

Kiu d liu

Kiu chui String

Ph ng thc ToString()

string myString = "Hello World"; string str = So nghiem = + n.ToString();

Kiu d liu

Mt s k t escape thng d ng

Escape Sequence \' \" \\ \0 \a \b \f \n \r \t \v

Character Single quote Double quote Backslash Null Alert Backspace Form feed New line Carriage return Tab character Vertical tab

Chuyn i kiu d liu

S thnh chui
int so = 10;

Chui thnh s

string chuoi = so.ToString(); string chuoi = 12345; double n = double.Parse(chuoi); double n = Convert.ToDouble(chuoi); int n = Convert.ToInt32(chuoi);

Khai bo bin, hng

int i;

Khai bo bin:

i = 0; int x = 10; y = 20; bool b = true;

Khai bo hng: const int a = 20;

Cu lnh lp

While
while (l<r) { cu lnh } do { cu lnh } while (l<r)

Do..While

For
for (int i=0; i<n; i++) { cu lnh }

Cu lnh lp

Foreach
double[] arr = {1.0, 2.1, 3.5, 4.2, 5.3}; foreach (double temp in arr) { sum += temp; } for (int i=0; i<arr.Length; i++) { double temp = arr[i]; sum += temp; }

Cu lnh iu kin

If..else
if ( a => b ) { max = a; } else { max = b; }

Switch
switch (x) { case 1 : ; break; case 2 : ; break; default : ; break; }

Cc ton t
Category Arithmetic Logical String concatenation Increment and decrement Assignment Shortcut operator Shortcut operator Operator +-*/% & | ^ ~ && || ! + ++ -= += -= *= /= %= &= |= ^= <<= >>= ++ , -++ , --

Vit ghi ch

// : ghi ch trn mt dng


int len;

/* */ : ghi ch trn nhiu dng


/* Nguoi tao : ... Ngay tao : */ void int HamMoi (...)

// chieu dai mang

Cc php ton

Thng qua lp Math

Mt s hm thng dng
Ly gi tr tuyt i

Ly tha

Int x = Math.Abs(-16); Int x = Math.Pow(2,3); // x = 2^3 Double x = Math.Sqrt(4); // x = 2; Double x = Math.Round(0.1234,2) // x= 0.12

Ly cn

Lm trn

L p trnh trn mi tr ng Windows

Mi trng VS2005

Trn Duy Hong tdhoang@hcmus.edu.vn

Ci t Visual Studio
B c 1 Ci t mi tr ng .NET 2005 B c 2 Ci t th vi n MSDN

Lm vic vi Solution
Solution
Project 1 Project 2 Project 2

HelloWorld
MyLibrary HelloWorld
MyLibrary: ng dng dll HelloWorld: ng dng Console

To mi mt project

M VS2005

Menu File New Project...


Project type : chn Visual C# Template : Window Application Name : tn d n Location : ni lu d n

To mi mt project

To mi mt project

To mi mt project

<<TextBox>> Name : txtSo1

<<Label>> Text : Tng

<<Button>> Name : btnCong Text : Cng

To mi mt project

Vit code x l cho Button Cng


Double click vo button cng

Mn hnh vit code s xut hin

To mi mt project
private void btnCong_Click(object sender, EventArgs e) { // Bc 1 : c d liu t control int so1 = Convert.ToInt32(txtSo1.Text); int so2 = Convert.ToInt32(txtSo2.Text); // Bc 2 : x l int tong = so1 + so2; // Bc 3 : xut kt qu ra control txtTong.Text = tong.ToString(); }

To mi mt project

Ch y ch ng trnh

Bm F5 chy chng trnh

Th o lu n

02/15/11

Trn Duy Hong - tdhoang@fit.hcmus.edu.vn

28/10

You might also like