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). 代碼示例:
class LightweightCustomer2
{3
public double TotalPurchases { get; set; }4
public string Name { get; private set; } // read-only5
public int CustomerID { get; private set; } // read-only6
}
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查看):
internal sealed class <>f__AnonymousType0<<ID>j__TPar, <Name>j__TPar>2
internal sealed class <>f__AnonymousType1<<Name>j__TPar, <ID>j__TPar>這里生成兩個internal泛型定義,用多了不知道會不會類型泛濫...
(3). 既然類型是在編譯時就已經確定下來了,何不自己去定義呢。相比之下,下面的代碼不會只會產生一個類型定義:
class Employee<T1,T2>
{
public T1 P1 { get; set; }
public T2 P2 { get; set; }//屬性名看起來沒有上面的漂亮
}
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.


浙公網安備 33010602011771號