Está en la página 1de 31

Lp trnh CSharp (C#)

GV. Bi Tn Lc btloc@fit.hcmuns.edu.vn
Ngn ng C#
1. C# l g?
2. .NET Framework
3. Ngn ng trung gian MSIL
4. c im ca ngn ng C#
5. Mi trng lp trnh v pht trin ng dng
6. Chng trnh u tin
Ngn ng C#
7. Khai bo bin hng
8. Kiu d liu
9. Console I/O
10. Vng lp
11. Cu lnh iu kin
12. Cc ton t

1.C# l g?
Ngn ng lp trnh hng i tng c thit k
da trn kinh nghim ca cc ngn ng hng i
tng khc: Visual Basic, C++, Java,
c thit k ring pht trin cng ngh
Microsofts .NET Framework
2..NET Framework
B khung pht trin ng dng;
Bn ngn ng chnh:C#, VB.NET, C++.NET, Jscript.NET
Common Language Runtime CLR (.NET Runtime):
tng t my o Java
B th vin Framework Class Library - FCL
2.1.Kin trc .NET Framework
3.Ngn ng trung gian MSIL
Qu trnh bin dch trong .NET theo 2 bc:
Dch m ngun (C#, VB.NET, C++.NET, JScript.NET)
thnh Microsoft Intermediate Language (MSIL).
Dch MSIL thnh m c th bi CLR.
Nh MSIL m mt ng dng vit bng ngn ng ny
c th s dng th vin (dng .dll) ca ngn ng
khc.
4.c im ca ngn ng C#
Khong 80 t kha
10 kiu d liu dng sn
H tr lp trnh cu trc, lp trnh hng i tng,
hng thnh phn (Component oriented)
C t kha khai bo dnh cho thuc tnh (property)
Cho php to su liu trc tip bn trong m ngun
(dng tool m ngun m NDoc pht sinh ra su liu)
H tr khi nim interface (tng t java)
C ch t ng dn rc (tng t java)
Truyn tham s kiu: in(), out, ref
5.Mi trng lp trnh v pht
trin ng dng
Ngun m Sharp Develop
Microsoft Visual Studio .NET 2005
B th vin v hng dn lp trnh MSDN 2007
ng dng dng:
Console
dll
Windows Form
Web

6.Chng trnh C# u tin
using System;

namespace MyFirstProject
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(Hello world");
Console.ReadLine();
}
}
}

7.Khai bo bin, hng
Khai bo bin:
int i;
i = 0;
int x = 10; y = 20;
bool b = true;
Khai bo hng:
const int a = 20;
7.1.Phm vi hot ng ca bin
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
} // bin i ra khi phm vi
// Chng ta c th khai bo thm bin i y
for (int i = 9; i >= 0; i--)
{
Console.WriteLine(i);
} // bin i ra khi phm vi y

7.1.Phm vi hot ng ca bin (tt)
int j = 20;
for (int i = 0; i < 10; i++)
{
int j = 30; // khng th thc thi - j vn cn trong
phm vi
Console.WriteLine(j + i);
}

8.Kiu d liu
Value Type - Kiu d liu c bn: kiu d liu khc
lp i tng
Reference Type - Kiu tham chiu: kiu d liu l lp
i tng
8.1.Value type s nguyn
Name CTS Type Description
Range
(min:max)
byte System.Byte
8-bit signed
integer
0:255 (0:2
8
-1)
ushort System.UInt16
16-bit signed
integer
0:65,535 (0:2
16
-1)
uint System.UInt32
32-bit signed
integer
0:4,294,967,295
(0:2
32
-1)
ulong System.UInt64
64-bit signed
integer
0:18,446,744,073,
709,551,615(0:
2
64
-1)
8.1Value type s nguyn
Name CTS Type Description Range (min:max)
sbyte System.SByte
8-bit signed
integer
-128:127 (-2
7
:2
7
-1)
short System.Int16
16-bit
signed
integer
-32,768:32,767 (-2
15
:2
15
-1)
int System.Int32
32-bit
signed
integer
-2,147,483,648:2,147,483,647 (-2
31
:2
31
-1)
long System.Int64
64-bit
signed
integer
-9,223,372,036,854,775,808:
9,223,372,036,854,775,807 (-2
63
:2
63
-1)
8.1.Value type s thc
Name CTS Type Description
Significant
Figures
Range
(approxim
ate)
Float System.Single
32-bit single-
precision floating-
point
7
1.5 10
-45
to
3.4 10
38

Double System.Double
64-bit double-
precision floating-
point
15/16
5.0 10
-324
to
1.7 10
308

decimal System.Decimal
128-bit high
precision decimal
notation
28
1.0 10-28 to
7.9 1028
V d
long x = 0x12ab;// ghi theo hexa
uint ui = 1234U;
long l = 1234L;
ulong ul = 1234UL;
float f = 12.3F;
decimal d = 12.30M ; //c th vit decimal d = 12.30m;
8.1Value type - Kiu Boolean &
char
Name CTS Type Value
Bool System.Boolean true or false
char System.Char
Represents a single 16-bit (Unicode)
character
Cc k t escape thng dng
Escape Sequence Character
\' Single quote
\" Double quote
\\ Backslash
\0 Null
\a Alert
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Tab character
\v Vertical tab
8.2.Reference Type
Lp i tng Object (System.Object): l lp tru
tng, l lp cha ca tt c cc lp. Khi nh ngha
mt lp A. Mc nhin A s ly Object lm lp cha.
Reference Type:
Kiu lp: Object, String, CHocSinh, CLopHoc,
9.Console I/O
Console.Write() - Vit mt gi tr ra ca s window
Console.WriteLine() - tng t trn nhng s t
ng xung hng khi kt thc lnh
Console.ReadLine() c mt chui t console
9.Console I/O (tt)
V d:
string s = Console.ReadLine();
Console.WriteLine(s);
int i = 10;
int j = 20;
Console.WriteLine("{0} plus {1} equals {2}", i, j, i + j);



9.Console I/O (tt)
V d: nhp s nguyn v s thc
int n;
string s = Console.ReadLine();
n = int.Parse(s);
double f;
s = Console.ReadLine();
f = double.Parse(s);
10.Vng lp
Vng lp:
do while: ging C/C++
while: ging C/C++
for: ging C/C++
foreach: khc C/C++
V d foreach:
foreach (int temp in arrayOfInts)
{
Console.WriteLine(temp);
}
11.Cu lnh iu kin
Cu lnh iu kin:
if: ging C/C++
switch: ging C/C++
12.Cc ton t
Category Operator
Arithmetic + - * / %
Logical & | ^ ~ && || !
String concatenation +
Increment and decrement ++ --
Bit shifting << >>
Comparison == != < > <= >=
Assignment = += -= *= /= %= &= |= ^= <<= >>=
12.Cc ton t (tt)
Category Operator
Member access (for objects and
structs)
.
Indexing (for arrays and indexers) []
Cast ()
Conditional (the Ternary Operator) ?:
Object Creation new
Type information sizeof (unsafe code only) is typeof as
Overflow exception control checked unchecked
Indirection and Address * -> & (unsafe code only) []
12.Cc ton t (tt)
Shortcut Operator Tng ng
x++, ++x x = x + 1
x--, --x x = x - 1
x += y x = x + y
x -= y x = x y
x *= y x = x * y
x /= y x = x / y
12.Cc ton t (tt)
Shortcut Operator Tng ng
x %= y x = x % y
x >>= y x = x >> y
x <<= y x = x << y
x &= y x = x & y
x |= y x = x | y
x ^= y x = x ^ y
Bi tp
Check li nhng g cn nm trong bi?
Nhm bi tp chng trnh ch c khi hm Main
Cc bi tp lm quen vi mi trng Visual Studtio.
Cc bi tp lm quen vi nhp/xut, kiu d liu c
bn, ton t, vng lp
Cc bi tp lm quen vi nhp/xut, kiu d liu c
bn, ton t, cu lnh iu kin

También podría gustarte