Bài giảng Lập trình trên Windows - Chương 1: C# cơ bản

C# cơ bản  
.Net framework  
2
Mechanism  
3
Hello world  
4
Artifact  
❑ Build => Error  
❑ Run vs Run without debugging  
❑ Release  
❑ Debug  
▪ Breakpoint & watch  
▪ Step in & Step over  
▪ Watch  
❑ Set as startup project  
❑ Open project location  
5
Types  
6
Variable  
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  
int x1 = x[0];  
// 1  
// Multi dimension array: same size in all item  
int [,] x = {{1, 2}, {3, 4}};  
int [,] x = {{1, 2}, {3, 4, 5}};  
int x2 = x[1, 0];  
// compile error  
// 3  
int [,,] x = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};  
7
Number casting  
// Smaller type in memory size integral  
// type to wider one  
byte b = 5; // implicit int to byte  
int i = b; // i = 5  
// Wider integral type to smaller one  
int i = 500;  
byte j = i; // compile error  
byte j = (byte)i; // i = 244  
// No explicit conversion for floating  
// point number  
8
Boxing / Unboxing  
❑ Boxing: convert value type to object  
❑ Unboxing: extract value type from object  
9
Minimum & maximum value  
Result?  
10  
Operation 1/2  
❑ References: . () [] new ->  
❑ Arithmetic: + ++ - -- * / % sizeof  
❑ Logical: & | ^ !  
❑ Conditional: && (&) || ! == != > >= < <=  
❑ Type verification: is as typeof  
❑ Bitwise: ~ >> <<  
❑ Assignment: = += -= *= /= %= &= |=  
^= >>= <<=  
❑ Selection: ?: ?? (not null)  
❑ Lambda expression definition: =>  
11  
Operation 2/2  
int i = 5;  
// Selection ?:  
string x = i == 5 ? "Yes": "No";  
int? x = 5;  
int y = x ?? 0; // Selection ?? operator  
int z = x; // Error: nullable type  
// nullable type  
// cannot assign to non-nullable type  
// Lambda expression – anonymous method  
(int x) => x * 2; <=>  
public int Double(int x){return x * 2}  
12  
String literal  
// Declaration and initialization  
// "\" start an "escape" code  
string s1 = "a\n", s2 = "\"",  
s3 = @"a\n", s4 = s3;  
// assignment  
s2 = s1;  
// concatenation  
s3 = s1 + s3;  
13  
Escape code  
\a Bell (alert)  
\b Backspace  
\f Formfeed  
\'  
\"  
\\  
\?  
Single quotation mark  
Double quotation mark  
Backslash  
\n New line  
Literal question mark  
ASCII character in  
octal notation  
\r Carriage return \ooo  
ASCII character in  
hexadecimal notation  
\t Horizontal tab \x hh  
\v Vertical tab  
\x hhhh Unicode in herxadeximal  
14  
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  
New York  
Chicago  
1940  
1940  
1940  
1940  
1,504,277  
7,454,995  
3,396,808  
1,623,452  
1950  
1950  
1950  
1950  
1,970,358  
7,891,957  
3,620,962  
1,849,568  
31.0 %  
5.9 %  
6.6 %  
Detroit  
13.9 %  
string s = $"{x} - {y}";  
15  
String concatenation  
❑ Use “+” character  
❑ Use StringBuilder  
❑ Question: what should we use, + or StringBuilder?  
16  
String Split  
❑ Exercise  
▪ Calculate sum of string numbers = “5, 3, 8, 11, -12, 3”  
▪ Split String fraction = “3/4” into int numerator and  
denominator  
What if we meet 3//4?  
17  
String format  
❑ Positive: right align, negative: left align  
18  
String search  
❑ Exercise  
▪ Given string s = “She sells seashells by the seashore.  
The shells she sells are seashells”  
▪ Calculate the number of occurrence of the word  
“sells” and “she”  
❑ Further reading: replace and regular expression  
19  
String as char array  
Faster!  
20  
Tải về để xem bản đầy đủ
pptx 34 trang myanh 27460 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình trên Windows - Chương 1: C# cơ bản", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pptxbai_giang_lap_trinh_tren_windows_chuong_1_c_co_ban.pptx