C#3.0语法糖学习笔记


0. Auto-Implemented Properties:
(1). Auto-implemented properties make property-declaration more concise when no additional logic is required
in the property accessors. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field can only be accessed through the property's get and set accessors.
(2). 代码示例:

1 class LightweightCustomer
2{
3    public double TotalPurchases getset; }
4    public string Name getprivate set; } // read-only
5    public int CustomerID getprivate set; } // read-only
6}



1. Object initializers
(1). Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor. The following example(参考3中的Anonymous Types) shows how to use an object initializer with a named type.
(2). 本质上是先调用无参构造函数创建一个临时对象,然后对临时对象对外公开(public/internal)的属性/字段赋值,最后将临时对象赋值给代码中声明的引用。由于这里需要调用无参构造函数,所以只能在具有无参构造函数(如果定义类型时,没有给类型定义构造函数,则编译器会自动给类添加一个无参构造函数)的类型实例化时使用Object initializers。


2. Collection Initializers
(1). Enables initialization of collections with an initialization list rather than specific calls to Add or another method.
(2). 本质上也只是new个临时collection,然后调用Add方法,最后将临时集合赋值给代码中声明的集合引用


3. Anonymous Types
(1). Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type.
(2). 示例:

var employee1 = new { ID = Guid.NewGuid(), Name = "happyhippy" };
var employee2 
= new { Name = "happyhippy", ID = Guid.NewGuid() };

本质上编译生成的类型定义(可以通过Reflector/IL DASM查看):

1 internal sealed class <>f__AnonymousType0<<ID>j__TPar, <Name>j__TPar>
2internal sealed class <>f__AnonymousType1<<Name>j__TPar, <ID>j__TPar>

这里生成两个internal泛型定义,用多了不知道会不会类型泛滥...
(3). 既然类型是在编译时就已经确定下来了,何不自己去定义呢。相比之下,下面的代码不会只会产生一个类型定义:

class Employee<T1,T2>
{
    
public T1 P1 getset; }
    
public T2 P2 getset; }//属性名看起来没有上面的漂亮
}

Employee
<Guid, string> employee1 = new Employee<Guid, string> { P1 = Guid.NewGuid(), P2 = "happyhippy" };
Employee
<string, Guid> employee2 = new Employee<string, Guid> { P1 = "happyhippy", P2 = Guid.NewGuid() };



4. Extension Methods
(1). Extend existing classes by using static methods that can be invoked by using instance method syntax.Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.
(2). 本质上是CSC编译生成对静态方法的调用的IL代码;使用时看起来有点像编译时多态
(3). 优先级:类型定义中的方法 > Client代码所在命名空间中定义的Extension Methods > 其他命名空间(前提:必须先import命名空间)中定义的Extension Methods;如果定义了优先级相同且签名相同的方法,会出现编译时错误。
(4). 示例,仅供演示用。
public static class ExtensionMethod
{
    public static string ToString(this string source)
    {
        return "3.5";
    }
}
使用:Console.WriteLine("3.0".ToString());
输出:3.0
解释:参考(3)中优先级的解释。


5. Lambda Expressions
(1). A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
(2). 在2.0中的Anonymous Method Delegate的基础上又包了一层糖,本质上都是编译生成Named Delegate。
(3). 语法:
(input parameters) => expression
(input parameters) => {statement;}

6. Partial Methods
(1). A partial class or struct may contain a partial method. One part of the class contains the signature of the method. An optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the method and all calls to the method are removed at compile time.
(2). MSDN上号称的作用:Partial methods are especially useful as a way to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method. Much like partial classes, partial methods enable code created by a code generator and code created by a human developer to work together without run-time costs.
(3). 示例:

// Definition in file1.cs
partial void onNameChanged();
// Implementation in file2.cs
partial void onNameChanged()
{
  
// method body
}

(4). N多限制:
标识为partial,返回值必须为void;
可以使用ref参数,但不能使用out参数;
partial methods默认的访问级别是private, 不能在上面应用virtual;
Partial methods上不能应用extern,因为方法体已经决定了它是否已定义及具体实现;
Partial methods上可以应用static或unsafe修饰符;
Partial methods可以是泛型方法;
Cannot make a delegate to a partial method.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值