You are on page 1of 5

Phn I . Tng quan v C# I . S lc v C# 1 . Ngn ng C# Ngn ng C# c pht trin bi i ng k s ca Microsoft, trong ngi dn u l Anders Hejlsberg v Scott Wiltamuth.

. C hai ngi ny iu l nhng ngi ni ting, trong Anders Hejlsberg c bit n l tc gi ca Turbo Pascal, mt ngn ng lp trnh PC ph bin. V ng ng u nhm thit k Borland Delphi, mt trong nhng thnh cng u tin ca vic xy dng mi trng pht trin tch hp (IDE) cho lp trnh client/server. Ngn ng C# kh n gin, ch khong 80 t kha v hn mi my kiu d liu c xy dng sn. Tuy nhin, ngn ng C# c ngha cao khi n thc thi nhng khi nim lp trnh hin i. C# bao gm tt c nhng h tr cho cu trc, thnh phn component, lp trnh hng i tng. Nhng tnh cht hin din trong mt ngn ng lp trnh hin i. V ngn ng C# hi nhng iu kin nh vy, hn na n c xy dng trn nn tng ca hai ngn ng mnh nht l C++ v Java. Phn ct li hay cn gi l tri tim ca bt c ngn ng lp trnh hng i tng l s h tr ca n cho vic nh ngha v lm vic vi nhng lp. Nhng lp th nh ngha nhng kiu d liu mi, cho php ngi pht trin m rng ngn ng to m hnh tt hn gii quyt vn . Ngn ng C# cha nhng t kha cho vic khai bo nhng kiu lp i tng mi v nhng phng thc hay thuc tnh ca lp, v cho vic thc thi ng gi, k tha, v a hnh, ba thuc tnh c bn ca bt c ngn ng lp trnh hng i tng. C# cng h tr giao din interface, n c xem nh mt cam kt vi mt lp cho nhng dch v m giao din quy nh. Trong ngn ng C#, mt lp ch c th k tha t duy nht mt lp cha, tc l khng cho a k tha nh trong ngn ng C++, tuy nhin mt lp c th thc thi nhiu giao din. Khi mt lp thc thi mt giao din th n s ha l n s cung cp chc nng thc thi giao din. Trong ngn ng C#, nhng cu trc cng c h tr, nhng khi nim v ng ngha ca n thay i khc vi C++. Trong C#, mt cu trc c gii hn, l kiu d liu nh gn, v khi to th hin th n yu cu t hn v h iu hnh v b nh so vi mt lp. Mt cu trc th khng th k tha t mt lp hay c k tha nhng mt cu trc c th thc thi mt giao din. 2 . Ti sao phi s dng ngn ng C# Nhiu ngi cho rng khng cn thit c mt ngn ng lp trnh mi. Java, C++, Perl, Microsoft Visual Basic, v nhng ngn ng khc c ngh rng cung cp tt c nhng chc nng cn thit. Ngn ng C# l mt ngn ng c dn xut t C v C++, nhng n c to t nn tng pht trin hn. Microsoft bt u vi cng vic trong C v C++ v thm vo nhng

c tnh mi lm cho ngn ng ny d s dng hn. Nhiu trong s nhng c tnh ny kh ging vi nhng c tnh c trong ngn ng Java. Khng dng li , Microsoft a ra mt s mc ch khi xy dng ngn ng ny. Nhng mc ch ny c c tm tt nh sau: C# l ngn ng n gin C# loi b mt vi s phc tp v ri rm ca nhng ngn ng nh Java v c++, bao gm vic loi b nhng macro, nhng template, a k tha, v lp c s o (virtual base class). Ngn ng C# n gin v n da trn nn tng C v C++. Nu chng ta thn thin vi C v C++ hoc thm ch l Java, chng ta s thy C# kh ging v din mo, c php, biu thc, ton t v nhng chc nng khc c ly trc tip t ngn ng C v C++, nhng n c ci tin lm cho ngn ng n gin hn. Mt vi trong cc s ci tin l loi b cc d tha, hay l thm vo nhng c php thay i. C# l ngn ng hin i Nhng c tnh nh l x l ngoi l, thu gom b nh t ng, nhng kiu d liu m rng, v bo mt m ngun l nhng c tnh c mong i trong mt ngn ng hin i. C# cha tt c nhng c tnh trn. Nu l ngi mi hc lp trnh c th chng ta s cm thy nhng c tnh trn phc tp v kh hiu. C# l ngn ng hng i tng Nhng c im chnh ca ngn ng hng i tng (Object-oriented language) l s ng gi (encapsulation), s k tha (inheritance), v a hnh (polymorphism). C# h tr tt c nhng c tnh trn. C# l ngn ng mnh m v cng mm do vi ngn ng C# chng ta ch b gii hn chnh bi bn thn hay l tr tng tng ca chng ta. Ngn ng ny khng t nhng rng buc ln nhng vic c th lm. C# c s dng cho nhiu cc d n khc nhau nh l to ra ng dng x l vn bn, ng dng ha, bn tnh, hay thm ch nhng trnh bin dch cho cc ngn ng khc. C# l ngn ng t t kha C# l ngn ng s dng gii hn nhng t kha. Phn ln cc t kha c s dng m t thng tin. Chng ta c th ngh rng mt ngn ng c nhiu t kha th s mnh hn. iu ny khng phi s tht, t nht l trong trng hp ngn ng C#, chng ta c th tm thy rng ngn ng ny c th c s dng lm bt c nhim v no. Bng sau lit k cc t kha ca ngn ng C#.

abstract as base bool break byte case catch char checked class const continue decimal

default delegate do double else enum event explicit extern false finally fixed float for

foreach goto if implicit in int interface internal is lock long namespace new null

object operator out override params private protected public readonly ref return sbyte sealed short

sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked

unsafe ushort using virtual volatile void while

Bng : T kha ca ngn ng C#. C# l ngn ng hng module M ngun C# c th c vit trong nhng phn c gi l nhng lp, nhng lp ny cha cc phng thc thnh vin ca n. Nhng lp v nhng phng thc c th c s dng li trong ng dng hay cc chng trnh khc. Bng cch truyn cc mu thng tin n nhng lp hay phng thc chng ta c th to ra nhng m ngun dng li c hiu qu. 3 . C# v nhng ngn ng khc Microsoft ni rng C# mang n sc mnh ca ngn ng C++ vi s d dng ca ngn ng Visual Basic. Mc d C# loi b mt vi cc c tnh ca C++, nhng b li n trnh c nhng li m thng gp trong ngn ng C++. iu ny c th tit kim c hng gi hay thm ch hng ngy trong vic hon tt mt chng trnh. C# cng t b tng a k tha nh trong C++. V s khc nhau khc l C# a thm thuc tnh vo trong mt lp ging nh trong Visual Basic. V nhng thnh vin ca lp c gi duy nht bng ton t . khc vi C++ c nhiu cch gi trong cc tnh hung khc nhau. Mt ngn ng khc rt mnh v ph bin l Java, ging nh C++ v C# c pht trin da trn C. Nu chng ta quyt nh s hc Java sau ny, chng ta s tm c nhiu ci m hc t C# c th c p dng. im ging nhau C# v Java l c hai cng bin dch ra m trung gian: C# bin dch ra MSIL cn Java bin dch ra bytecode. Sau chng c thc hin bng cch thng dch hoc bin dch just-in-time trong tng my o tng ng. Tuy nhin, trong ngn ng C# nhiu h tr c a ra bin dch m ngn ng trung gian sang m my. C# cha

nhiu kiu d liu c bn hn Java v cng cho php nhiu s m rng vi kiu d liu gi tr Tng t nh Java, C# cng t b tnh a k tha trong mt lp, tuy nhin m hnh k tha n ny c m rng bi tnh a k tha nhiu giao din. II . Nn tng ngn ng C# 1 . Kiu d liu C# l ngn ng lp trnh mnh v kiu d liu,tc l phi khai bo kiu ca mi i tng khi to (kiu s nguyn, s thc, kiu chui, kiu iu khin...) v trnh bin dch s gip cho ngi lp trnh khng b li khi ch cho php mt loi kiu d liu c th c gn cho cc kiu d liu khc. Kiu d liu ca mt i tng l mt tn hiu trnh bin dch nhn bit kch thc ca mt i tng (kiu int c kch thc l 4 byte) v kh nng ca n (nh mt i tng button c th v, phn ng khi nhn,...). Kiu d liu xy dng sn Kiu C# byte char bool sbyte short ushort int uint float double S byte 1 2 1 1 2 2 4 4 4 8 Kiu .NET Byte Char Boolean Sbyte Int16 Uint16 Int32 Uint32 Single Double M t S nguyn dng khng du t 0-255 K t Unicode Gi tr logic true/ false S nguyn c du ( t -128 n 127) S nguyn c du gi tr t -32768 n 32767. S nguyn khng du: 0 65.535 S nguyn c du 2.147.483.647 v 2.147.483.647 S nguyn khng du: 0 4.294.967.295 Kiu du chm ng, gi tr xp x t 3,4E38 n 3,4E+38, vi 7 ch s c ngha.. Kiu du chm ng c chnh xc gp i, gi tr xp x t 1,7E-308 n 1,7E+308, vi 15,16 ch s c ngha. C chnh xc n 28 con s v gi tr thp phn, c dng trong tnh ton ti chnh, kiu ny i hi phi c hu t m hay M theo sau gi tr. Kiu s nguyn c du c gi tr trong khong : -9.223.370.036.854.775.808 n 9.223.372.036.854.775.807 S nguyn khng du t 0 n 0xffffffffffffffff

decimal

Decimal

long ulong

8 8

Int64 Uint64

Chuyn i cc kiu d liu Nhng i tng ca mt kiu d liu ny c th c chuyn sang nhng i tng ca mt kiu d liu khc thng qua c ch chuyn i tng minh hay ngm nh. Chuyn

i nhm nh c thc hin mt cch t ng, trnh bin dch s thc hin cng vic ny. Cn chuyn i tng minh din ra khi chng ta gn p mt gi tr cho kiu d liu khc. 2 . Bin v hng Bin Mt bin l mt vng lu tr vi mt kiu d liu. Bin c th c gn gi tr v cng c th thay i gi tr khi thc hin cc lnh trong chng trnh. to mt bin chng ta phi khai bo kiu ca bin v gn cho bin mt tn duy nht. Bin c th c khi to gi tr ngay khi c khai bo, hay n cng c th c gn mt gi tr mi vo bt c lc no trong chng trnh. C php C# sau y khai bo mt bin : [ t kha ] <kiu d liu> <tn bin> ; VD: public int i ; Hng Hng cng l mt bin nhng gi tr ca hng khng thay i. Bin l cng c rt mnh, tuy nhin khi lm vic vi mt gi tr c nh ngha l khng thay i, ta phi m bo gi tr ca n khng c thay i trong sut chng trnh. ngn nga vic gn gi tr khc, ta phi s dng bin kiu hng. C php C# sau y khai bo mt hng : [ t kha ] <const> <kiu d liu> < tn hng>=<gi tr> ; VD: public const int k=100;