Professional Documents
Culture Documents
2 NgonnguC
2 NgonnguC
C#
Ngn ng lp trnh thun hng i tng
using System;
class Hello
{
public static void Main()
{
Console.WriteLine("Hello, World");
}
}
Convert.ToBoolean();
Convert.ToByte();
Convert.ToInt16();
Byte.Parse();
Int64.Parse();
Double.Parse()
Kiu d liu nh sn
Kiu C# S byte Kiu .NET M t
byte 1 Byte S nguyn dng
khng du t 0-255
char 2 Char K t Unicode
bool 1 Boolean Gi tr logic true/ false
sbyte 1 Sbyte S nguyn c du
( t -128 n 127)
short 2 Int16 S nguyn c du gi
tr t -32768 n 32767
ushort 2 UInt16 S nguyn khng du
0 65.535
16 Lp Trnh mi trng Windows
24/02/2009
Kiu d liu nh sn
Kiu C# S byte Kiu .NET M t
int 4 Int32 S nguyn c du
- 2.147.483.647 n
2.147.483.647
uint 4 Uint32 S nguyn khng du
0 4.294.967.295
float 4 Single Kiu du chm ng,
3,4E-38 n 3,4E+38,
vi 7 ch s c ngha..
double 8 Double Kiu du chm ng c
chnh xc gp i
1,7E-308 n 1,7E+308
vi 15,16 ch s c ngha.
17 Lp Trnh mi trng Windows
24/02/2009
Kiu d liu nh sn
Kiu C# S byte Kiu .NET M t
decimal 8 Decimal C chnh xc n 28 con
s
dng trong tnh ton ti chnh
phi c hu t m hay M
theo sau gi tr
long 8 Int64 Kiu s nguyn c du
-9.223.370.036.854.775.808
n
9.223.372.036.854.775.807
ulong 8 Uint64 S nguyn khng du
t 0 n 0xffffffffffffffff
18 Lp Trnh mi trng Windows
24/02/2009
Kiu d liu nh sn
Console.WriteLine("sbyte:{0} to {1},sbyte.MinValue,sbyte.MaxValue);
Console.WriteLine("byte:{0} to {1}", byte.MinValue, byte.MaxValue);
Console.WriteLine("short:{0} to {1}", short.MinValue, short.MaxValue);
Console.WriteLine("ushort:{0} to {1}", ushort.MinValue,
ushort.MaxValue);
Console.WriteLine("int:{0} to {1}", int.MinValue, int.MaxValue);
Console.WriteLine("long:{0} to {1}", long.MinValue, long.MaxValue);
Console.WriteLine("decimal:{0} to {1}", decimal.MinValue,
decimal.MaxValue);
Console.ReadLine();
Enum(eration) kiu tp hp
Days d = Days.Mon;
switch (d) {
case Days.Tue:
case Days.Wed:
}
B ? 55
B 55
A ? 105
B ?
struct
struct : value type (class : reference type)
Dng cho cc i tng nh nh
Point, Rectangle, Color,
Box v Unbox
i qua li gia value type v reference
type.
Box: value => reference (object).
Thng dng trong cc hm, cu trc d
liu s dng tham s l kiu object tng
qut.
int i = 123;
object o = i; // implicit boxing
object o = (object) i; // explicit boxing
int j = (int) o; // unboxing
Box v Unbox
void MyMethod()
{
int num1 = 7, num2 = 9;
Swap(ref num1, ref num2);
// num1 = 9, num2 = 7
}
void Swap(ref int x, ref int y)
{
int temp = x; x = y; y = temp;
}
35 Lp Trnh mi trng Windows
24/02/2009
Keyword out
void MyMethod()
{ uninitialise
d
int num1 = 7, num2;
Subtraction(num1, out num2);
// num1 = 7, num2 = 5
}
void Subtraction(int x, out int y)
{
y = x - 2;
// y must be assigned a value
}
36 Lp Trnh mi trng Windows
24/02/2009
Keyword params
void MyMethod()
{
int sum = Addition(1, 2, 3); // sum = 6
}
int Addition(params int[] integers)
{
int result = 0;
for (int i = 0; i < integers.Length; i++)
result += integers[i];
return result;
}
37 Lp Trnh mi trng Windows
24/02/2009
Pht biu chn
switch (expression)
{
case constant-expression:
statement
jump-statement
[default:
statement
jump-statement]
24/02/2009
} 39 Lp Trnh mi trng Windows
Pht biu lp
Pht biu vng lp trong C# bao gm do, for,
foreach, while.
Vng lp do
do
statement
while (expression);
Vng lp while
while (expression)
statement
Vng lp foreach in
foreach (type identifier in expression)
statement
Vng lp foreach lp li mt nhm pht biu cho
mi phn t trong mng hay tp i tng.
Pht biu dng duyt qua tt c cc phn t
trong mng hay tp i tng v thc thi mt tp
lnh
41 Lp Trnh mi trng Windows
24/02/2009
Pht biu nhy
Pht biu nhy s c s dng khi
chng trnh mun chuyn i iu
khin.
Pht biu nhy: break, continue, default,
goto, return
Class1.Finalize()
{
// Thc hin mt s cng vic
base.Finalize();
}
class Student {
protected DateTime _Birthday;
class Student {
protected DateTime _Birthday;
public int Birthday {
get {
return _Birthday;
}
set {
if ()
throw new
_Birthday = value;
}
}
}
chau.Birthday = new DateTime(2007,09,23);
Console.Writeline(Birthday: {0}, chau.Birthday);
Hng i tng
public class BankAccount {
protected string ID;
protected string Owner; Fields
protected decimal _Balance;
public BankAccount(string ID, string Owner) {
this.ID = ID;
this.Owner = Owner;
this._Balance = 0;
}
public void Deposit(decimal Amount) {
_Balance+=Amount;
}
public void Withdraw(decimal Amount) {
_Balance-=Amount; // what if Amount > Balance?
}
public decimal Balance {
get {
Thuc tnh ch c
return _Balance;
} Read-only property
}
};
73 Lp Trnh mi trng Windows
24/02/2009
Hng i tng
class Program
{
static void Main(string[] args)
{
BankAccount myAcct = new Account(
"100120023003", "Nguyen Van An");
myAcct.Deposit(1000);
myAcct.Withdraw(100);
Console.WriteLine("Balance: {0}", myAcct.Balance);
//myAcct.Balance=10000;
Console.ReadLine();
}
}
Indexer
Cho php to ra cc thuc tnh ging nh array (nhng cch
ci t bn trong khng nht thit dng array). Lu l ch
mc khng nht thit phi l integer.
C th c nhiu ch mc
vd: Marks[string SubjectID, string SemesterID]
class Student {
protected string StudentID;
protected Database MarkDB;
public double Marks[string SubjectID] {
get {
return MarkDB.GetMark(StudentID,SubjectID);
}
set {
MarDB.UpdateMark(StudentID,value);
}
}
}
Console.Writeline(Physic mark: {0},chau.Marks[physic]);
75 Lp Trnh mi trng Windows
Chng hm (overload)
Khng chp nhn hai phng thc ch
khc nhau v kiu tr v.
Khng chp nhn hai phng thc ch
khc nhau v c tnh ca mt thng s
ang c khai bo nh ref hay out.
{
Console.WriteLine(Drawing Window at {0}, {1}, top, left);
}
// C hai bin thnh vin private do hai bin ny s khng
thy bn trong lp dn xut.
private int top;
private int left;
}
Lp Window
public virtual void DrawWindow() // m phng v ca s
{
Console.WriteLine(Drawing Window at {0}, {1}, top, left);
}
Lp Listbox
public override void DrawWindow()
{
base.DrawWindow();
Console.WriteLine( ListBox write: {0}, mListBoxContents);
}
Abstract class
public abstract class BankAccount {
public abstract bool IsSufficientFund(decimal Amount);
public abstract void AddInterest();
firstFraction + secondFraction
Fraction.operator+(firstFraction, secondFraction)
interface IStorable
{
void Read();
void Write(object);
}
Interface vs Abstract
Abstract: phn nh tt c c im (k c cu trc ni ti)
chung nht ca mt tp i tng no .
interface IMovable
{
public 3DPoint Position { get; set; }
public double MaxSpeed { get; set; }
public MoveTo(3DPoint newPosition);
}
Interface vs Abstract
Mt class ch c tha k t mt lp c s
Enter Main...
Enter Func1...
Entering try block...
Enter Func2...
Exception caught and handled.
Exit Func1...
Exit Main...
V d
class Test
{
static void Main(string[] args)
{
Test t = new Test();
t.TestFunc();
}
public double DoDivide(double a, double b)
{
if ( b == 0)
throw new System.DivideByZeroException();
if ( a == 0)
throw new System.ArithmeticException();
return a/b;
}