生活随笔
收集整理的这篇文章主要介绍了
C#3.0新特性小结(2)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
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); } } }
转载于:https://blog.51cto.com/chenkai/765463
总结
以上是生活随笔为你收集整理的C#3.0新特性小结(2)的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。