C# căn bản
Lập trình ứng dụng quản lý
1
Ngơ Chánh Đức
Nội dung
.NET Framework
C# cơ bản
Coding convention
.NET Framework
Giới thiệu
Ra đời năm 2002 cùng với bộ công cụ lập trình Visual
Studio .NET
Sử dụng kỹ thuật hướng đối tượng
Hỗ trợ chuẩn Unicode
Thi hành trên máy ảo CLR (Common Language
Runtime)
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
Kiến trúc .NET Framework
Cơ chế thực thi
Kiến trúc .NET Framework
Ứ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)
Đọc thêm
Kiến trúc .NET Framework: />
C# căn bản
Giớ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 tồ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ó đi mở rộng là .cs
Sử dụng Visual Studio để lập trình
Hello world
using System;
class Hello
{
static void Main()
{
Console.WriteLine("Hello, World");
}
}
Hello.cs
Bắt đầu với C#
Biên dịch
Chạy gỡ rối (Debug) và chạy không gỡ rối (Run without
debug)
Chế độ Release/Debug
Chạy gỡ rối
▪Breakpoint
▪Step In / Step out / Step over
▪Watch / Auto watch / Local watch
Cấ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)
Kiểu dữ liệu
Kiểu
tham chiếu
Kiểu
giá trị
Cá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
Cá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
Các kiểu dữ liệu số
Floating point
▪float : 32 bits, range from 1.5 × 10−45 to 3.4 ×
1038, 7-digit precision
▪double : 64 bits, range from 5.0 × 10−324 to
1.7 × 10308, 15-digit precision
Decimal
▪decimal : 128 bits, range is at least –7.9 ×
10−28 to 7.9 × 1028, with at least 28-digit
precision
Biế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
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}};
// compile error
int x2 = x[1, 0];
// 3
int [,,] x = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
Ép kiểu số
// 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
Boxing / Unboxing
Boxing: chuyển kiểu giá trị object
Unboxing: trích xuất kiểu giá trị từ object
Toán tử
References: . () [] new ->
Arithmetic: + ++ - -- * / % sizeof
Logical: & | ^ !
Conditional: && (&) || ! == != > >= < <=
Type verification: is as typeof
Bitwise: ~ >> <<
Assignment: = += -= *= /= %= &= |= ^= >>=
<<=
Selection: ?: ?? (not null)
Lambda expression definition: =>
Toá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}
Cấu trúc điều khiển
Branching
▪Selection: ?: ??
▪ if… else if … else
▪ switch … case … default
Iteration
▪ for
▪ foreach
▪ do while
▪ while
Ignore & breaking
▪ continue
▪ break
Chuỗi 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;