C# 2.0 new features (.NET framework SDK): 1.Partial classes allow class implementation across more than one file. This permits breaking down very large classes, or is useful if some parts of a class are automatically generated. 2.Generics or parameterized types. This is a .NET 2.0 feature supported by C#. Unlike C++ templates, .NET parameterized types are instantiated at runtime rather than by the compiler; hence they can be cross-language whereas C++ templates cannot. They support some features not supported directly by C++ templates such as type constraints on generic parameters by use of interfaces. On the other hand, C# does not support non-type generic parameters. Unlike generics in Java, .NET generics use reification to make parameterized types first-class objects in the CLI Virtual Machine, which allows for optimizations and preservation of the type information. 3.Static classes that cannot be instantiated, and that only allow static members. This is similar to the concept of module in many procedural languages. 4.A new form of iterator that provides generator functionality, using a yield return construct similar to yield in Python. // Method that takes an iterable input (possibly an array) and returns all even numbers. public static IEnumerable GetEven(IEnumerable numbers) { foreach (int i in numbers) { if (i % 2 == 0) yield return i; } }
5.Anonymous delegates providing closure functionality.
public void Foo(object parameter) { // ... ThreadPool.QueueUserWorkItem(delegate { // anonymous delegates have full access to local variables of the enclosing method if (parameter == ...) { // ... } // ... }); } 6.Covariance and contravariance for signatures of delegates 7. The accessibility of property accessors can be set independently. Example:
string status = string.Empty; public string Status { get { return status; } // anyone can get value of this property, protected set { status = value; } // but only derived classes can change it }
8.Nullable value types (denoted by a question mark, e.g. int? i = null;) which add null to the set of allowed values for any value type. This provides improved interaction with SQL databases, which can have nullable columns of types corresponding to C# primitive types: an SQL INTEGER NULL column type directly translates to the C# int?. int? i = null; object o = i; if (o == null) Console.WriteLine("Correct behaviour - you have a runtime version from September 2005 or later"); else Console.WriteLine("Incorrect behaviour - you are running a pre-release runtime (from before September)"); When copied into objects, the official release boxes values from Nullable instances, so null values and null references are considered equal. 9. Coalesce operator: (??) returns the first of its operands which is not null object nullObj = null; object obj = new Object(); return nullObj ?? obj; // returns obj
The primary use of this operator is to assign a nullable type to a non-nullable type with an easy syntax:
int? i = null; int j = i ?? 0; // Unless i is null, initialize j to i. Else (if i is null), initialize j to 0. |
0 comments:
Post a Comment