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.


本文转自Silent Void博客园博客,原文链接:http://www.cnblogs.com/happyhippy/archive/2008/01/17/1043410.html,如需转载请自行联系原作者

相关文章
|
3月前
|
C#
halcon联合c#、WPF学习笔记三(dispatcherTimer实时相机显示)
halcon联合c#、WPF学习笔记三(dispatcherTimer实时相机显示)
65 1
halcon联合c#、WPF学习笔记三(dispatcherTimer实时相机显示)
|
3月前
|
C#
halcon联合c#、WPF学习笔记二(简单案例)
halcon联合c#、WPF学习笔记二(简单案例)
99 0
|
3月前
|
C# C++
halcon联合c#、WPF学习笔记一(WPF配置halcon)
halcon联合c#、WPF学习笔记一(WPF配置halcon)
117 1
|
C# C语言
C#学习笔记22: 面向对象编程入门精讲(下)
C#学习笔记22: 面向对象编程入门精讲
C#学习笔记22: 面向对象编程入门精讲(下)
|
XML SQL 开发框架
C#十种语法糖
C#十种语法糖
116 0
C#十种语法糖
c#编程:事件delegate学习笔记-3
c#编程:事件delegate学习笔记-3
|
缓存 开发框架 运维
C#好代码学习笔记(1):文件操作、 读取文件、Debug/Trace类、Conditional条件编译、CLS
C#好代码学习笔记(1):文件操作、 读取文件、Debug/Trace类、Conditional条件编译、CLS
196 0
|
开发框架 并行计算 .NET
改善C#程序的157个建议——建议84学习笔记:使用PLINQ
改善C#程序的157个建议——建议84学习笔记:使用PLINQ
102 0
|
编译器 C#
改善C#程序的157建议——建议42学习笔记:使用泛型参数兼容泛型接口的不可变性。
改善C#程序的157建议——建议42学习笔记:使用泛型参数兼容泛型接口的不可变性。
101 0