C# căn bảnLập trình ứng dụng quản lý 1 Ngô Chánh Đức... Bao gồm hơn 5000 lớp đối tượng Cung cấp bộ trình điều khiển đồ sộ giao diện đồ họa Hỗ trợ dịch vụ quản lý bộ nhớ và nhiều dị
Trang 1C# căn bản
Lập trình ứng dụng quản lý
1 Ngô Chánh Đức
Trang 2Nội dung
.NET Framework
C# cơ bản
Coding convention
Trang 3.NET Framework
Trang 4 Bao gồm hơn 5000 lớp đối tượng
Cung cấp bộ trình điều khiển đồ sộ (giao diện đồ họa)
Hỗ trợ dịch vụ quản lý bộ nhớ và nhiều dịch vụ khác
Trang 5Kiến trúc NET Framework
Trang 6Cơ chế thực thi
Trang 7Kiến trúc NET Framework
Trang 8Ứng dụng NET Framework
Ứng dụng dòng lệnh (Console Application)
Ứng dụng WinForm (Windows Form Application)
Ứng dụng WPF (WPF Application)
Ứng dụng web (ASP.NET Web Form Application)
Thư viện DLL (Class Library)
Trang 9Đọc thêm
Kiến trúc NET Framework:
http://www.c-and-architecture/
Trang 10sharpcorner.com/UploadFile/puranindia/net-framework-C# căn bản
Trang 11Giới thiệu
Phát triển bởi Microsoft
Xuất hiện năm 2000
Phiên bản mới nhất là C# 7.0
Là ngôn ngữ lập trình cấp cao, hiện đại, hướng đối tượng và an toàn kiểu (type-safe)
Có nguồn gốc từ C
Cú pháp có phần quen thuộc với C, C++, Java, …
Tập tin mã nguồn có đuôi mở rộng là cs
Sử dụng Visual Studio để lập trình
Trang 13▪Step In / Step out / Step over
▪Watch / Auto watch / Local watch
Trang 14Cấu trúc lập trình
Chương trình (Program)
Tên miền (namespace)
Kiểu dữ liệu (type)
Thành viên (member)
Hợp ngữ (assembly)
Trang 15Kiểu dữ liệu
Kiểu
Trang 16Các kiểu dữ liệu số
Signed Integral
▪sbyte: 8 bits, range from -128 to 127
▪short: 16 bits, range from -32,768 to 32,767
▪int : 32 bits, range from -2,147,483,648 to
2,147,483,647
▪long : 64 bits, range from –
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
Trang 17Các kiểu dữ liệu số
Unsigned integral
▪byte : 8 bits, range from 0 to 255
▪ushort : 16 bits, range from 0 to 65,535
▪uint : 32 bits, range from 0 to 4,294,967,295
▪ulong : 64 bits, range from 0 to
18,446,744,073,709,551,615
Trang 18precision
Trang 19Biến dữ liệu
int i; // Simple variable
// Many variables can have the same type declaration
float x, y, z;
byte x = 1, y, z = 3;
string name = "new name" ;
// Array: Indexed by an integer from Zero
int x[] = {1, 2, 3}; // compile error
int [] x = {1, 2, 3}; char [] y = new char [6]; // OK
Trang 20byte j = (byte)i; // i = 244
// No explicit conversion for floating// point number
Trang 21Boxing / Unboxing
Boxing: chuyển kiểu giá trị object
Unboxing: trích xuất kiểu giá trị từ object
Trang 22Toán tử
References: () [] new ->
Arithmetic: + ++ - * / % sizeof
Logical: & | ^ !
Conditional: && (&) || ! == != > >= < <=
Type verification: is as typeof
Bitwise: ~ >> <<
Assignment: = += -= *= /= %= &= |= ^= >>=
<<=
Selection: ?: ?? (not null)
Lambda expression definition: =>
Trang 23Toán tử
int i = 5;
// Selection ?:
string x = i == 5 ? "Yes": "No";
int ? x = 5; // nullable type
int y = x ?? 0; // Selection ?? operator
int z = x; // Error: nullable type
// cannot assign to non-nullable type
// Lambda expression – anonymous method
( int x) => x * 2; <=>
public int Double(int x){return x * 2}
Trang 24Cấu trúc điều khiển
Branching
▪Selection: ?: ??
▪ if… else if … else
▪ switch … case … default
Trang 26Escape code
\a Bell (alert) \' Single quotation mark
\b Backspace \" Double quotation mark
\r Carriage return \ooo ASCII character in
Trang 27Định dạng chuỗi – String format
string header = String Format(
"{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n" , // pattern string
"City" , "Year" , "Population" , "Change (%)" ); // argument list
string body = String Format(
"{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}" ,
Name, BaseYear, BasePopulation, ObserveYear, ObservePopulation,
ObservePopulation – BasePopulation)/ ( double ) BasePopulation);
// Sample output
City Year Population Year Population Change (%)
Los Angeles 1940 1,504,277 1950 1,970,358 31.0 % New York 1940 7,454,995 1950 7,891,957 5.9 % Chicago 1940 3,396,808 1950 3,620,962 6.6 % Detroit 1940 1,623,452 1950 1,849,568 13.9 %
-string s = $"{x} - {y}";
Trang 28Kết chuỗi
Sử dụng “+”
Sử dụng StringBuilder
▪ types/stringbuilder
https://docs.microsoft.com/en-us/dotnet/standard/base- Câu hỏi: nên kết bằng dấu “+” hay StringBuilder?
Trang 29Tách chuỗi
Phương thức String.Split
Trang 30Tìm kiếm chuỗi
Sử dụng phương thức String.IndexOf /
String.LastIndexOf
Trang 31Mảng ký tự
Trang 32Bài tập chuỗi
1 Read a string and give statistics about the number of
occurrence for each of the word in the string
2 Normalize a string of full name and print out on the
screen: no more than one spaces between words, the first letter is capitalized meanwhile the rest are in lower case, no space in the beginning and the end of the
string
3 Split String fullpath = “C:\Documents\Photos\Test.jpg”
into a) Containing directory b) File name c) Extension
Trang 33using System;
…
DateTime a = new DateTime();
// Construction complete with year, month, day,
// hour, minute, second, millisecond
DateTime b = new DateTime(2013, 06, 15, 15,
28, 31, 927);
// Current time
DateTime c = DateTime.Now;
Trang 34Chuyển String thành DataTime
Trang 35Chuyển String thành DataTime
// Specify exactly how to interpret the string
IFormatProvider culture = new
System.Globalization.CultureInfo( "fr-FR" , true );
// Alternate choice: If the string has been input
by an end user, you might want to format it
according to the current culture:
// IFormatProvider culture =
System.Threading.Thread.CurrentThread.CurrentCultur e;
DateTime dt2 = DateTime.Parse(dateTime, culture,
System.Globalization.DateTimeStyles.AssumeLocal);
Console.WriteLine( "Year: {0}, Month: {1}, Day: {2}, Hour: {3}, Minute: {4}, Second: {5}, Millisecond:
dt2.Minute, dt2.Second, dt2.Millisecond
Trang 36Lớp đối tượng (Class) và Cấu trúc (Struct)
Đều giúp đóng gói dữ liệu (data) + hành vi (behavior)
Lớp đối tượng (Class) kiểu tham chiếu
Cấu trúc (Struct) kiểu giá trị
Nhìn ở góc độ Lập trình hướng đối tượng
▪Class biểu diễn đối tượng phức tạp
▪Struct biểu diễn dữ liệu phức tạp
Trang 37Ví dụ về Class và Struct
Trang 40Đọc thêm về Class và Struct
https://docs.microsoft.com/en-
us/dotnet/csharp/programming-guide/classes-and-structs/
https://www.tutorialspoint.com/csharp/csharp_classes.htm
Trang 41Khai báo và định nghĩa Phương thức
// undefined parameter number
public void Multiply( params int [] list){}
Multiply(); // OK
Multiply(1); // OK too
Multiply(1, 2, 3, 3, 4); // still OK
Multiply( new int [] {1, 2, 3, 3, 4}); // error
// predefined parameter number
public void Multiply( int [] list){}
Multiply(); // Error
Multiply(1); // Error too
Multiply( new int [] {1, 2, 3, 3, 4}); // OK
Trang 42Tham số đầu vào/đầu ra của phương thức
public void Add( int a, int b, int c){
Trang 44Mảng nhiều chiều (multi-dimensional array)
// Two-dimensional array
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified
int[,] array2Da = new int[4, 2] { { 1, 2 }, {
3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements
string[,] array2Db = new string[3, 2] { {
"one", "two" }, { "three", "four" }, { "five",
"six" } };
Trang 45Mảng lỏm chỏm (Jagged array)
// inside item can have different size
//array of array
// error: no init for jagged array allowed
int[][] x = new int[2][];
x[0] = x0;
x[1] = x1;
Trang 46Truy xuất phần tử của mảng
int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int i in numbers) {
System.Console.Write("{0} ", i);
}
// Output: 4 5 6 1 2 3 -2 -1 0
Trang 47Truy xuất phần tử của mảng
int[,] numbers2D = new int[3, 2] {
Trang 48Đọc thêm
http://csharp.net-tutorials.com/
Làm các bài tập trên www.learncs.org
Trang 49Basic coding convention
Trang 50if for do while
Trước và sau có một khoảng trắng
Luôn có cặp ngoặc cho dù chỉ có 1 dòng lệnh
Ngoặc ở dòng kế / trên cùng dòng (java / php / js)
Sau if luôn có else
Trang 51Ghi chú
Ghi chú cho mỗi đoạn lệnh
Ghi chú đối số của hàm
Trang 52Tên hàm
Bắt đầu bằng động từ
Private: camelCase
Public: PascalCase
Ex: isPrime, doSomething
Kiểm tra phần tử tồn tại trong mảng??