生活随笔
收集整理的這篇文章主要介紹了
C#3.0新特性小结(2)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
c#3.0中除了自動屬性外,還添加:
- 隱含類型局部變量(Local Variable Type Inference)
- 匿名類型(Anonymous Types)
- 對象與集合初始化器(Object and Collection Initializers)
代碼演示實例:
?????????public?static?void?TestArray() ?????????{ ??????????????????????????var?intArray?=?new[]?{?120,?110,?119?}; ?????????????var?strArray?=?new[]?{?"TreeyLee",?"JFlyZhao",?"JFlyingchen"?}; ??????????????var?objArray?=?new[]?{? ???????????????????????????new?{username="馬鵬飛",userpass="AdminDoucment"}, ?????????????new?{username="周靜",userpass="周界"} ????????????}; ???????????????????????????var?seta?=intArray[0]; ?????????????var?setb?=?strArray[1]; ?????????????var?setc?=?objArray[1].userpass;??????????????????????????Console.WriteLine("seta:"?+?seta?+?"\nsetb:"?+setb+?"\nsetc:"?+?setc); ??????????} ???????????????????public?class?TotalTestAnnoy ???{ ??????????????public?string?username?{?get;?set;?} ???????public?string?userpass?{?get;?set;?} ???????public?int?age?{?get;?set;?} ???????? ??????????????public?static?void?TestMethods() ???????{ ??????????????????????List<TotalTestAnnoy>?TotalList?=?new?List<TotalTestAnnoy>?{? ?????????????????????????new?TotalTestAnnoy{username="老顧",userpass="laogu",age=21}, ?????????????new?TotalTestAnnoy{username="周靜",userpass="zhoujing",age=26}, ?????????????new?TotalTestAnnoy{username="陳凱",userpass="chenkai",age=21} ???????????}; ???????????????????????var?GetTotalList?=?from?newtotallist?in?TotalList ??????????????????????????????where?newtotallist.age?==?21??????????????????????????????select?new?{?newname?=?newtotallist.username,?newpass?=?newtotallist.userpass?}; ??????????????????????foreach(var?getfirst?in?GetTotalList) ???????????{ ??????????????????????????????Console.WriteLine("定義新實例newname:"+getfirst.newname+"\nnewpass:\n"+getfirst.newpass); ???????????} ????????} ???????? ???}? ?
轉(zhuǎn)載于:https://blog.51cto.com/chenkai/765463
總結(jié)
以上是生活随笔為你收集整理的C#3.0新特性小结(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。