龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

介绍下.Net FrameWork 3.5新语法特性

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
.Net FrameWork 3.5有一些新语法,介绍一下: 自动属性(Auto-Implemented Properties) 隐含类型局部变量(Local Variable Type Inference) 匿名类型(Anonymous Types) 对象与集合初始化器(Object and Collection Initial

.Net FrameWork 3.5有一些新语法,介绍一下:

自动属性(Auto-Implemented Properties)
隐含类型局部变量(Local Variable Type Inference)
匿名类型(Anonymous Types)
对象与集合初始化器(Object and Collection Initializers)
扩展方法(Extension Methods)
Lambda表达式和Lambda表达式树 (Lambda Expression and Lambda Expression Trees)
语言级集成查询Linq系列支持(Language INtegrated Query)
 

自动属性举例:

    private String name;

    public String Name

    {

        get { return name; }

        set { name = value; }

}

.NET 3.5下的新写法:public String Name { set; get; }

 

隐含类型局部变量举例:

        var url = "http://www.xueit.com";     //url被识别String

        var dt = new DataTable();            //dt被识别为DataTable

        var status = false;                    //status被识别为Boolen

 

匿名类型举例:

            var site1 = new { url = " http://1.2.3.4 ", title = "BBS", author = "Alex", qq = "4111852" };
            var site2 = new { url = " http://www.xueit.com ", title = "Title", author = "Alex", qq = "4111852" };
            site1.url = site2.url;

 

对象与集合初始化器举例:

            User user = new User();
            user.Id = 1;
            user.Name = "Alex";
            user.Age = 22;

.NET 3.5下新的写法:

            var user = new User { Id = 1, Name = "Alex", Age = 22 };

 

扩展方法举例:

    public static int Int(this HttpRequest request, string key)

    {

        return int.Parse(request[key]);

}

调用方法:int id = Request.Int("id");


精彩图集

赞助商链接