C#是一個語言,.net是一個平台,上面支持用C#或者VB .Net寫代碼。
另外,C#不但可以開發基於.net的應用程式,也可以開發基於WinForm的程式,這就是區別。
. NET 是 Microsoft 的 XML Web 服務平台。不論作業系統或程式語言有何差別,XML Web 服務能使應用程式在 Internet 上傳輸和共享數據。
Microsoft® .NET 平台包含廣泛的產品系列,它們都是基於 XML 和 Internet 行業標準構建,提供從開發、管理、使用到體驗 XML Web 服務的每一方面。XML Web 服務將成為您今天正在使用的 Microsoft 的應用程式、工具和伺服器的一部分 — 並且將要打造出全新的產品以滿足您所有業務需求。
更具體地說,Microsoft 正在五個方面創建 .NET 平台,即工具、伺服器、XML Web 服務、客戶端和 .NET 體驗。
若是單純以概念來說,你可以把 .NET當做一個工作平台一般,它是一個開發環境的基底,提供了解其運作的相關機制那是有助於你利用它來開發。C#和 .NET主要是套用在網際網路。
基本介紹
- 中文名:C#.net
- 外文名:C#.net
- 所屬公司:微軟公司
- 讀做:C-sharp
起源
C#2.0新特性
泛型
匿名方法
疊代器
局部類型
空屬類型
靜態類
C#3.0新特性
classLocalVariables:AppRunner.AbstractApplication{publicoverridevoidRun(){varintValue=5;varstringValue="Thisisastring";varcustomClass=newLocalVariables();varintArray=newint[3]{1,2,3};foreach(varvalueinintArray)Console.WriteLine(value);}}上面的代碼將被解析成:classLocalVariables:AppRunner.AbstractApplication{publicoverridevoidRun(){intintValue=5;stringstringValue="Thisisastring";LocalVariablescustomClass=newLocalVariables();int[]intArray=newint[3];foreach(intvalueinintArray)Console.WriteLine(value);}}
classAnonymousType:AppRunner.AbstractApplication{publicoverridevoidRun(){varanonymousType1=new{CardNumber="10001",Name="van’s",Sex=true};Console.WriteLine(anonymousType1.CardNumber);Console.WriteLinevaranonymousType2=new{CardNumber="10002",Name="martin",Sex=true};anonymousType2=anonymousType1;}}
classAnonymousTypeArray:AppRunner.AbstractApplication{publicoverridevoidRun(){varintArray=new[]{1,2,3,4,5};vardoubleArray=new[]{3.14,1.414};varanonymousTypeArray=new[]{new{Name="van’s",Sex=false,Arg=22},new{Name="martin",Sex=true,Arg=23}};Console.WriteLine(intArray);Console.WriteLine(doubleArray);Console.WriteLine(anonymousTypeArray[0].Name);}}
classPoint{publicintX{get;set;}publicintY{get;set;}publicoverridestringToString(){return"("+X.ToString()+","+Y.ToString()+")";}}classRectangle{publicPointP1{get;set;}publicPointP2{get;set;}publicRectangle(){P1=newPoint();P2=newPoint();}publicoverridestringToString(){return"P1:"+P1+",P2:"+P2;}}classObjectBuilder:AppRunner.AbstractApplication{publicoverridevoidRun(){PointthePoint=newPoint(){X=1,Y=2};Console.WriteLine("Point(X,Y)=",thePoint);RectangletheRectangle=newRectangle(){P1={X=1,Y=1},P2={X=100,Y=200}};Console.WriteLine(theRectangle);}}
classCollectionInitializer:AppRunner.AbstractApplication{classStringCollection:ICollection<string>{publicvoidAdd(stringitem){Console.WriteLine(item);}//OtherICollection<T>Members}publicoverridevoidRun(){StringCollectionstrings=newStringCollection(){"Van's","Brog","Vicky"};}}
delegateTAddDelegate<T>(Ta,Tb);classLambdaExpression:AppRunner.AbstractApplication{publicstaticeventEventHandlerMyEvent;publicoverridevoidRun(){MyEvent+=delegate(objects,EventArgse){Console.WriteLine(s);};MyEvent+=(s,e)=>{Console.WriteLine(s);};MyEvent(this,null);AddDelegate<string>add=(a,b)=>a+b;Console.WriteLine(add("Lambda","Expression"));}}
staticclassExtensions{publicstaticintToInt32(thisstringsource){returnInt32.Parse(source);}publicstaticT[]Slice<T>(thisT[]source,intindex,intcount){if(index<0||count<0||index+count>source.Length){thrownewArgumentException();}T[]result=newT[count];Array.Copy(source,index,result,0,count);returnresult;}}classExtensionMethods:AppRunner.AbstractApplication{publicoverridevoidRun(){stringnumber="123";Console.WriteLine(number.ToInt32());int[]intArray=newint[]{1,2,3};intArray=intArray.Slice(1,2);foreach(variinintArray)Console.WriteLine(i);}}
classLinqExpression:AppRunner.AbstractApplication{publicoverridevoidRun(){//定義匿名數組persons,並為其賦初值varpersons=new[]{new{Name="Van's",Sex=false,Age=22},new{Name="Martin",Sex=true,Age=30},new{Name="Jerry",Sex=false,Age=24},new{Name="Brog",Sex=false,Age=25},new{Name="Vicky",Sex=true,Age=20}};/*執行簡單Linq查詢檢索所有年齡在24歲以內的人查詢結果放在results變數中results變數的類型與數組persons相同*/varresults=frompinpersonswherep.Age<=24selectp;foreach(varpersoninresults){Console.WriteLineConsole.WriteLine();//定義匿名數組customers,並為其賦初值//該數組是匿名類型的varcustomers=new[]{new{Name="Van's",City="China",Orders=new[]{new{OrderNo=0,OrderName="C#ProgrammingLanguage(SecondEdition)",OrderDate=newDateTime(2007,9,5)},new{OrderNo=1,OrderName="HeadFirstDesignPatterns(ChineseEdition)",OrderDate=newDateTime(2007,9,15)},new{OrderNo=2,OrderName=Unleashed2.0(ChineseEdition)",OrderDate=newDateTime(2007,09,18)},new{OrderNo=3,OrderName="TheC++ProgrammingLangauge(SpecialEdition)",OrderDate=newDateTime(2002,9,20)}}},new{Name="Brog",City="China",Orders=new[]{new{OrderNo=0,OrderName="C#ProgrammingLanguage(SecondEdition)",OrderDate=newDateTime(2007,9,15)}}},new{Name="Vicky",City="London",Orders=new[]{new{OrderNo=0,OrderName="C++ProgrammingLanguage(SpecialEdition)",OrderDate=newDateTime(2007,9,20)}}}};/*
*/varsomeCustomers=fromcincustomerswherec.City=="China"fromoinc.Orderswhereo.OrderDate.Year>=2007selectnew{o.OrderNo,o.OrderDate,o.OrderName};foreach(varcustomerinsomeCustomers){Console.WriteLine(customer.OrderName+","+customer.OrderDate.ToString("D"));}}}