Bài giảng Lập trình C Sharp - Giới thiệu C#

Value Type & Reference Type

Boxing & Unboxing

Giới thiệu lớp String

Giới thiệu lớp StringBuilder

Giới thiệu lớp ArrayList

Các dạng Dialog

Stream (Luồng dữ liệu)

 

 

 

ppt37 trang | Chuyên mục: Visual C# | Chia sẻ: dkS00TYs | Lượt xem: 2849 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng Lập trình C Sharp - Giới thiệu C#, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
GIỚI THIỆU C# (tt) GV: Phạm Anh Phương Email: paphuong@fit.hcmuns.edu.vn Chủ đề Value Type & Reference Type Boxing & Unboxing Giới thiệu lớp String Giới thiệu lớp StringBuilder Giới thiệu lớp ArrayList Các dạng Dialog Stream (Luồng dữ liệu) Value Type and Reference Type Value Type: Là những kiểu dữ liệu cơ sở được map trực tiếp vào FCL như int32 được map vào System.Int32 hoặc double được map vào System.double Giá trị Value Type được lưu trữ trên stack và lớp được kế thừa từ System.ValueType Ví dụ: Int x = 10; Double y = 30.2; System.ValueType a = 30; Reference Type Biến được cấp phát trên vùng nhớ Heap. Tất cả các lớp trong thư viện .NET hoặc do người dùng tạo ra đều là Reference Type. Dùng toán tử new để cấp phát vùng nhớ. Ví dụ: Object a = new ArrayList(); CHocSinh hs = new CHocSinh(); Value Type and Reference Type Boxing: Ép từ Value Type to Reference Type Ví dụ: Int32 x = 10;  object o = x ;  // Implicit boxing Console.WriteLine("The Object o = {0}",o); // prints out 10 //----------------------------------------------------------- Int32 x = 10;  object o = (object) x; // Explicit Boxing Console.WriteLine("The object o = {0}",o); // prints out 10 Boxing & Unboxing  Unboxing: Ép từ Reference Type sang Value Type Ví dụ: 	Int32 x = 5; // declaring Int32       Int64 y = 0; // declaring Int64 double       object o = x; // Implicit Boxing       y = (Int64)o; // Explicit boxing to double       Console.WriteLine("y={0}",y);        Boxing & Unboxing Giới thiệu lớp String Namespace: System Dùng để thể hiện dạng chuỗi trong .NET Bất cứ thao tác nào liên quan đến chuỗi ta sẽ dùng lớp string để lưu trữ và xử lý. 	Ví dụ: 	string str = “Hello World”; Giới thiệu lớp String Các hàm và property Giới thiệu lớp String (Sinh viên dùng thư viện MSDN để tra cứu thêm các hàm khác cũng như xem ví dụ mẫu của từng hàm) Ví dụ 1 : In ra từng kí tự trong chuỗi 	string str = “Hello World”; 	for(int j = 0; j ,,,, 	arr1 chứa 2 phần tử , Giới thiệu lớp String Ví dụ 7: Thay thế chuỗi 	string str = “hello world”; 	string res = str.Replace(“world”, “Lan”); Kết quả: 	res có kết quả là “hello Lan” Ví dụ 8: Lấy chuỗi con 	string str = “hello world”; 	string res = str.SubString(0, 2); Kết quả: 	res có kết quả là “he” Giới thiệu lớp String Ví dụ 7: Gán đường dẫn 	string str = “D:\\Thu muc 1\\Thu muc 2”; Hoặc 	string str = @”D:\Thu muc 1\Thu muc 2”; Ví dụ 8: Khởi tạo định dạng chuỗi 	int x = 10, y = 20; 	string str = string.Format(“{0} + {1} = {2}”,x,y, x+ y); 	//str = “10 + 20 = 30” Giới thiệu lớp StringBuilder Namespace: System.Text Khi thao tác trên chuỗi có sử dụng thường xuyên các hàm Replace, Insert, Append thì ta nên sử dụng StringBuilder thay vì dùng string để tăng tốc độ xử lý. Các hàm phổ biến: Replace, Insert, Append Giới thiệu lớp StringBuilder Ví dụ : Thay thế chuỗi 	StringBuilder sb = new StringBuilder(“Hello”); 	sb.Replace (“e”, “aaa”); //sb = “Haaallo” 	sb.Insert(1,”KKK”); //sb = “HaKKKaallo” 	string res = sb.ToString(); //res = “Haaallo” Giới thiệu lớp ArrayList Namespace: System.Collections Dùng để thể hiện mảng 1 chiều của bất kì lọai dữ liệu nào vì ArrayList thao tác trên lớp cơ sở Object. 	 Ví dụ: 	ArrayList arr = new ArrayList(); 	arr.Add(1); 	arr.Add(2); 	ArrayList dsHS = new ArrayList(); 	dsHS.Add (new CHocSinh(“Lan”)); 	dsHS.Add (new CHocSinh(“Diep”)); Giới thiệu lớp ArrayList Các hàm và property Giới thiệu lớp ArrayList Ví dụ 1: Thêm và lấy phần tử dạng Value Type 	ArrayList dsDiem = new ArrayList(); 	dsDiem.Add(1); 	dsDiem.Add(2); 	dsDiem.Add(3); 	for (int j = 0; j < dsDiem.Count; j ++) 	{ 	int item = (int) dsDiem[j]; 	Console.WriteLine (item); 	} Giới thiệu lớp ArrayList Ví dụ 2: Thêm và lấy phần tử dạng Reference Type 	ArrayList dsPhanSo = new ArrayList(); 	dsPhanSo.Add(new CPhanSo(1,2)); 	dsPhanSo.Add(new CPhanSo(3,4)); 	dsPhanSo.Add(new CPhanSo(5,6)); 	for (int j = 0; j < dsPhanSo.Count; j ++) 	{ 	 CPhanSo ps = (CPhanSo) dsPhanSo [j]; 	 Console.WriteLine (ps.LayTu() + “/” + ps.LayMau()); 	} Giới thiệu lớp ArrayList Ví dụ 3: 	ArrayList dsDiem = new ArrayList(); 	dsDiem.Add(1); 	dsDiem.Add(2); 	int vitri = dsDiem.IndexOf (1); //Tìm vị trí 1 trong mảng 	dsDiem.Insert(1,10); //dsDiem = {1,10,2} 	dsDiem.RemoveAt(1); //Xóa phần tử tại vị trí 1 	dsPhanSo.Clear(); //Xóa tất cả phần tử trong mảng 	int[] arrDiem = (int[]) (dsDiem.ToArray (typeof(string))); 	//Trả về mảng int Các dạng Dialog Thư viện FCL xây dựng sẵn các dialog như mở file, save file, chọn màu, … Các dạng Dialog 	+ MessageBox: Dùng để xuất thông báo đến người dùng 	+ OpenFileDialog: Dùng để chọn mở 1 file. 	+ SaveFileDialog: Dùng để chọn 1 file để lưu 	+ ColorDialog: Chọn màu,.. 	+ … MessageBox Dùng để xuất 1 thông báo đến người dùng: Cú pháp: 	DialogResult MessageBox.Show (…) DialogResult là 1 kiểu enum cho biết người dùng chọn giá trị gì nếu dialog này có các nút như YES/NO/CANCEl MessageBox Ví dụ1: MessageBox.Show("Hello World", 	"Thi Nghiem", 	MessageBoxButtons.YesNo, 	MessageBoxIcon.Information); MessageBox Ví dụ 2: Nhận kết quả chọn YES/NO từ người dùng	 DialogResult res = MessageBox.Show(“2 lớn hơn 1", 	"Thi Nghiem", 	MessageBoxButtons.YesNo, 	MessageBoxIcon.Information); 	if (res == DialogResult.Yes) 	Console.Write("Chọn YES"); 	else 	Console.Write("Chọn NO"); OpenFileDialog Dialog này dùng để chọn file đường dẫn mà người dùng muốn mở Các hàm và thuộc tính 	 OpenFileDialog 	OpenFileDialog openFileDialog1 = new OpenFileDialog(); 	openFileDialog1.InitialDirectory = "c:\\" ; 	openFileDialog1.Filter = "txt files (*.txt)|*.txt| ini files (*.ini)|*.ini" ; 	openFileDialog1.FilterIndex = 2 ; openFileDialog1.RestoreDirectory = true ; 	if(openFileDialog1.ShowDialog() == DialogResult.OK) 	{ 	string fileName = openFileDialog1.FileName; 	} SaveFileDialog Đặt tên file muốn lưu và lấy đường dẫn của file này. Các hàm và property: Tương tự OpenFileDialog Stream Namespace: System.IO Khi muốn đọc hay ghi dữ liệu vào/ra tập tin hay muốn truyền dữ liệu từ máy này sang máy khác, ta phải tổ chức dữ liệu theo cấu trúc tuần tự các byte hay các gói tin Trong phạm vi bài này, ta sẽ dùng Stream để thao tác đọc/ghi file. Thao tác file Namespace: System.IO Thư viện: 	StreamReader 	StreamWritter 	StringReader 	StringWritter 	File 	.. Có 2 dạng file : Văn bản (Text) và Binary Thao tác file Kiểm tra file tồn tại: 	File.Exists (string path) Ví dụ: 	string path = @“C:\1.txt” 	if (File.Exists (path)) 	Console.WriteLine(“Tồn tại”); 	else 	Console.WriteLine(“Không tồn tại”); Đọc file văn bản Ví dụ 1: Đọc file văn bản try { 	using (StreamReader sr = new StreamReader("TestFile.txt")) 	{ 	String line; 	//Đọc và xuất từng dòng cho đến khi hết file. 	while ((line = sr.ReadLine()) != null) //sr.ReadToEnd() 	{ 	Console.WriteLine(line); 	} 	} } catch (Exception e) { 	Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } Ghi file văn bản Ví dụ 2: Ghi file văn bản try { 	using (StreamWriter sw = new StreamWriter("TestFile.txt")) { 	sw.Write("This is the "); sw.WriteLine("header for the file."); 	sw.Write("The date is: "); sw.WriteLine(DateTime.Now.ToString()); } } catch (Exception e) { 	Console.WriteLine("The file could not be written:"); Console.WriteLine(e.Message); } Ghi file binary (nhị phân) Ví dụ 3: Đọc file binary 	FileStream t = new FileStream("D:\\1.data", 	FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(t); CHocSinh hs = new CHocSinh("Lan", 10); CHocSinh hs1 = new CHocSinh("Diep", 30); 	bw.Write(hs.TUOI); 	 bw.Write(hs.HOTEN); 	bw.Write(hs1.TUOI); 	bw.Write(hs1.HOTEN); 	bw.Close(); t.Close(); Đọc file binary (nhị phân) Ví dụ 4: Đọc file binary 	FileStream t = new FileStream("D:\\1.data", FileMode.Open); BinaryReader br = new BinaryReader(t); ArrayList dsHS = new ArrayList(); while (br.PeekChar() != -1) { int tuoi = br.ReadInt32(); string hoten = br.ReadString(); CHocSinh hs = new CHocSinh(hoten, tuoi); dsHS.Add(hs); } br.Close(); //Đóng stream t.Close(); //Đóng stream HẾT 

File đính kèm:

  • pptBài giảng Lập trình C Sharp - Giới thiệu C#.ppt