Tải bản đầy đủ - 0 (trang)
Chapter 1. C# and .NET Programming

Chapter 1. C# and .NET Programming

Tải bản đầy đủ - 0trang

1.1C#and.NET

Inthepast,youmighthavelearnedalanguagelikeCorJavawithout

muchconcernabouttheplatformonwhichyouwouldbeprogramming.

Thesecross-platformlanguageswereascomfortableonaUnixboxas

theywereonaPCrunningWindows.

C#,however,wascreatedspecificallyfor.NET.While.NETmaybecome

cross-platformsomedaysoonaUnixportisreportedlyintheoffingfor

nowtheoverwhelmingmajorityof.NETprogramswillbewrittentorunon

amachinerunning.NET.Atthetimeofthiswriting,thatmeansa

Windowsmachine.



1.2The.NETPlatform

WhenMicrosoftannouncedC#inJuly2000,itsunveilingwaspartofa

muchlargerevent:theannouncementofthe.NETplatform.The.NET

platformisadevelopmentframeworkthatprovidesanewwaytocreate

Windowsapplications.However,.NETgoesbeyondtraditionalWindows

programmingtofacilitatecreatingwebapplicationsquicklyandeasily.

Microsoftsaysitisdevoting80%ofitsresearchanddevelopmentbudget

to.NETanditsassociatedtechnologies.Theresultsofthiscommitment

areimpressive.Foronething,thescopeof.NETishuge.Theplatform

consistsofthreeseparateproductgroups:

Asetoflanguages,includingC#andVisualBasic.NET;asetof

developmenttools,includingVisualStudio.NET;andpowerfultools

forbuildingapplications,includingtheCommonLanguageRuntime

(CLR),aplatformforcompiling,debugging,andexecuting.NET

applications.

Asetof.NETEnterpriseServers,formerlyknownasSQLServer

2000,Exchange2000,BizTalk2000,andsoon,thatprovide

specializedfunctionalityforrelationaldatastorage,email,B2B

commerce,etc.

New.NET-enablednon-PCdevices,fromcellphonestogame

boxes.

TheC#languagecanbeusedtodevelopthreetypesofapplicationsyou

canrunonyourWindowscomputer:

Consoleapplications,whichdisplaynographics

Windowsapplications,whichusethestandardWindowsinterface

Webapplications,whichcanbeaccessedwithabrowser



ThisbookwillfocusprimarilyonthebasicsoftheC#language,mostly

usingsimpleconsoleapplicationstoillustratelanguagefundamentals.



1.3The.NETFramework

Centraltothe.NETplatformisadevelopmentenvironmentknownasthe

.NETFramework.TheFrameworkspecifieshow.NETprogramming

constructssuchasintrinsictypes,classes,andinterfacesare

implemented.Youwilllearnabouttheseconstructsinthechapters

ahead.

The.NETFrameworksitsontopofanyflavoroftheWindowsoperating

system.ThemostimportantcomponentsoftheFrameworkarethe

CommonLanguageRuntime(CLR),describedintheprecedingsection,

andtheFrameworkClassLibrary(FCL),whichprovidesanenormous

numberofpredefinedtypesorclassesforyoutouseinyourprograms.

YouwilllearnhowtodefineyourownclassesinChapter8.Complete

coverageofalltheFCLclassesisbeyondthescopeofthisbook.For

moreinformation,seeC#inaNutshell(O'Reilly&Associates).



1.4TheC#Language

TheC#languageisdisarminglysimple,butC#ishighlyexpressivewhen

itcomestoimplementingmodernprogrammingconcepts.C#includesall

thesupportforstructured,component-based,object-oriented

programmingthatoneexpectsofamodernlanguagebuiltonthe

shouldersofC++andJava.

AsmallteamledbytwodistinguishedMicrosoftengineers,Anders

HejlsbergandScottWiltamuth,developedtheC#language.Hejlsbergis

alsoknownforcreatingTurboPascal,apopularlanguageforPC

programming,andforleadingtheteamthatdesignedBorlandDelphi,

oneofthefirstsuccessfulIntegratedDevelopmentEnvironmentsfor

client/serverprogramming.

ThegoalofC#istoprovideasimple,safe,object-oriented,Internetcentric,high-performancelanguagefor.NETdevelopment.C#issimple

becausetherearerelativelyfewkeywords.Thismakesiteasytolearn

andeasytoadapttoyourspecificneeds.

Keywordsarespecialwordsreservedbythelanguagethat

haveaspecificmeaningwithinallC#programs.Keywords

includeif,while,andfor.You'lllearnaboutthesekeywords

inthecomingchapters.



C#isconsideredsafebecauseitprovidessupportinthelanguagetofind

bugsearlyinthedevelopmentprocess.Thismakesforcodethatis

easiertomaintainandprogramsthataremorereliable.

C#wasdesigned,fromtheverystart,tosupportobject-oriented

programming.Thisbookwillexplainnotonlyhowtowriteobject-oriented

programs,butalsowhyobject-orientedprogramminghasbecomeso

popular.Theshortansweristhis:programsarebecomingincreasingly

complex,andobject-orientedprogrammingtechniqueshelpyoumanage



thatcomplexity.

C#wasdesignedfor.NET,and.NETwasdesignedfordevelopingweb

andweb-awareprograms.TheInternetisaprimaryresourceinmost

.NETapplications.

Finally,C#wasdesignedforprofessionalhigh-performance

programming.



1.5TheStructureofC#Applications

Atthemostfundamentallevel,aC#applicationconsistsofsourcecode.

Sourcecodeishuman-readabletextwritteninatexteditor.Atexteditor

islikeawordprocessor,butitputsnospecialcharactersintothefileto

supportformatting,onlythetext.AclassictexteditorisNotepad.

Example1-1showsanexampleofaverysimplesourcecodefile.

Example1-1.Asourcecodefile

namespaceNotePad

{

classHelloWorld

{

//everyconsoleappstartswithMain

staticvoidMain()

{

System.Console.WriteLine("Helloworld!");

}

}

}

ThisprogramisexplainedindetailinChapter2.Fornow,observethat

theprogramitselfisreadable;itisinnormaltext.Thewordsmaybe



strangeandthelayoutunusual,buttherearenospecialcharactersjust

thenormaltextproducedbyyourkeyboard.

Onceyouwriteyourprograminaneditor,youmustcompileit.Forthat

youneedacompiler.YouwilllearnhowtousetheC#compilerin

Chapter2.Oncecompiled,yourprogrammustberunandtested.

WhileyoucanperformallofthesetasksusingNotepad(oranothertext

editor)andvariouscommand-linetools,yourprogramminglifewillbe

mucheasierifyouusetheIntegratedDevelopmentEnvironment(IDE)

calledVisualStudio.NET.VS.NETwasdesignedwith.NETdevelopment

inmindandgreatlysimplifiesthewritingofC#programcode.



1.6TheDevelopmentEnvironment

TheVisualStudio.NETIntegratedDevelopmentEnvironmentprovides

enormousadvantagestotheC#programmer.Thisbooktacitlyassumes

thatyou'lluseVisualStudio.NETforyourwork.However,thediscussion

focusesmoreonthelanguageandtheplatformthanonthetools.

Nonetheless,Chapter4providesagoodintroductiontotheIDEinsome

detail.Chapter10returnstotheIDEtoexaminethedebugger,whichwill

helpyoufindandcorrectproblemsinyourcode.



Chapter2.GettingStartedwithC#

YoucanuseC#tocreatethreedifferenttypesofprograms:

Webapplications

Windowsapplications

Consoleapplications

The.NETplatformisweb-centric.TheC#languagewasdevelopedto

allow.NETprogrammerstocreateverylarge,powerful,high-qualityweb

applicationsquicklyandeasily.The.NETtechnologyforcreatingweb

applicationsiscalledASP.NET.

ASP.NET,thenextgenerationfromASP(ActiveServerPages),is

composedoftwoMicrosoftdevelopmenttechnologies:WebFormsand

WebServices.Whilethedevelopmentoffullyrealizedwebapplications

usingthesetechnologiesisbeyondthescopeofthisbook,learningthe

basicsoftheC#languagewillcertainlygetyoustartedintheright

direction.C#isgenerallyacknowledgedtobethelanguageofchoicefor

ASP.NETdevelopment.

Typically,you'llcreateanASP.NETapplicationwhenyouwantyour

programtobeavailabletoendusersonanyplatform(e.g.,Windows,

Mac,Unix).ByservingyourapplicationovertheWeb,enduserscan

accessyourprogramwithanybrowser.

Whenyouwanttherichnessandpowerofanativeapplicationrunning

directlyontheWindowsplatform,alternativelyyoumightcreatea

desktop-boundWindowsapplication.The.NETtoolsforbuilding

WindowsapplicationsarecalledWindowsForms;adetailedanalysisof

thistechnologyisalsobeyondthescopeofthisbook.

However,ifyoudon'tneedaGraphicalUserInterface(GUI)andjust

wanttowriteasimpleapplicationthattalkstoaconsolewindow(i.e.,



whatweusedtocallaDOSbox),youmightconsidercreatingaconsole

application.Thisbookmakesextensiveuseofconsoleapplicationsto

illustratethebasicsoftheC#language.

Web,Windows,andconsoleapplicationsaredescribedandillustratedin

thefollowingpages.

Consoleapplications

Aconsoleapplicationrunsinaconsolewindow,asshowninFigure

2-1.Aconsolewindow(orDOSbox)providessimpletext-based

output.

Figure2-1.Aconsoleapplication



Consoleapplicationsareveryhelpfulwhenlearningalanguagebecause

theystripawaythedistractionoftheGraphicalUserInterface.Rather

thanspendingyourtimecreatingcomplexwindowingapplications,you

canfocusonthedetailsofthelanguageconstructs,suchashowyou

createclassesandmethods,howyoubranchbasedonruntime

conditions,andhowyouloop.Allthesetopicswillbecoveredindetailin

comingchapters.

Windowsapplications

AWindowsapplicationrunsonaPC'sdesktop.Youarealready

familiarwithWindowsapplicationssuchasMicrosoftWordorExcel.

Windowsapplicationsaremuchmorecomplexthanconsole

applicationsandcantakeadvantageofthefullsuiteofmenus,

controls,andotherwidgetsyou'vecometoexpectinamodern

desktopapplication.Figure2-2showstheoutputofasimple

windowsapplication.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 1. C# and .NET Programming

Tải bản đầy đủ ngay(0 tr)

×