1
B tr 1
Mt s ñim khác
ca C#sovi C++
C#Simplified/Session1/2 of45
Chương trình C#ñơn gin
/* This is the Hello world Program in C# */
using System;
class HelloWorldDemo
{
public static void Main()
{
Console.WriteLine (“This is the Hello
World program”);
}
}
2
C#Simplified/Session1/3 of45
Khai o bi"n
AccessModifier DataType Variable
Public
Private
Protected
int
string
float
C#Simplified/Session1/4 of45
Bi"n trùng tên v*i t+ khóa,s/
d0ng @
using System;
class VariableDemo
{
public static void Main()
{
string @string;
@string = ”
string is a keyword but used as a
variable name in this example”;
Console.WriteLine (@string);
}
}
3
C#Simplified/Session1/5 of45
C#cung c2p nhi3u ki4u d5 li6u,
c ki4u s7 r2t l*n
boolval1=true;
boolval2=false;
Logicbool
charval='h'; t=char
floatval=1.23F;Th=c 64bitfloat
byteval=12;bytebyte
intval=12;Nguyên 32bitint
strings=“hello”;Xâustring
objecto=null;Ki4u d5 li6u sIobject
d tKiu dliu
trong C#
C#Simplified/Session1/6 of45
Khai o mng
Ki4u[]ten=newKi4u[s7_phNn_t/]
d0:
int[]a=newint[200];
String[]s=newString[20];
4
C#Simplified/Session1/7 of45
Vào /Ratrong C#
V*i Vng d0ng n giao ti"p (console)
S/ d0ng ñ7i tưYng System.Console v*i
các phương thVc ñZc/ghi hayñưYc s/
d0ng nh2t
Console.ReadLine()
Console.WriteLine()
C#Simplified/Session1/8 of45
{i}, i = 0, 1, … trong xâu ký ttham sthnhtñưc gi giành ch. Ký
tgiành ch{i} s ñưc thay thbi giá trca tham sthi trong danh
sách khi in.
Vào /Ratrong C#] d0
using System;
class TestDefaultValues
{
static void Main()
{
int number, result;
number=5;
result=100 * number;
Console.WriteLine (“Result is {0} when 100
is multiplied by number {1}”, result, number);
}
}
5
C#Simplified/Session1/9 of45
C
2
u
tr
ú
c
ñi
3
khi
4
n
switch
cho
phép s/ d0ng b2t kỳ giá trb ñ4
làm giá trb sosánh
switch(thXuly)
{
case “NGHI_HOC”:
Console.Write(“Ban da nghi hoc”);
break;
case “DI_HOC_MUON”:
Console.Write(“Ban di hoc muon”);
break;
default:
Console.Write(“Ban di hoc rat tot”);
break;
}
Giá trso sánh vi các case trong dnày là xâu t
C#Simplified/Session1/10 of45
L6nh lcp foreach(1)
S/ d0ng ñ4 lcp duy6t các phNn t/
trong collectionhocc array
pháp ]