Friday, May 27, 2011

3.5 language features

Auto Implemented Properties:
Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects.

-Object initializer
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

private class Cat
{
    // Auto-implemented properties.
    public int Age { get; set; }
    public string Name { get; set; }
}

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

-Collection initialize
Collection initializers let you specify one or more element intializers when you initialize a collection class that implements IEnumerable.
List digits = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List digits2 = new List { 0 + 1, 12 % 3, MakeInt() };


List cats = new List
{
    new Cat(){ Name = "Sylvester", Age=8 },
    new Cat(){ Name = "Whiskers", Age=2 },
    new Cat(){ Name = "Sasha", Age=14 }
};


Extension Methods:
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.Extension methods are a special kind of static method.

Extension methods are defined as static methods but are called 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

Implicit  local variable type inference:
Developers are allowed to declare the variables without mentioning the types by using the key word var.The compiler infers the type through its initialized values.
var i=10;
 i += 1;
 i = "Hello";

-Lamda expression:

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates  or expression tree types.

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies  the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is  read "x goes to x times x." This expression can be assigned to a delegate type as follows:
delegate int del(int i);
static void Main(string[] args)
{
    del myDelegate = x => x * x;
    int j = myDelegate(5); //j = 25
}

Query expressions:
One common query language that is used to query any different data source.This common query language is none other than LINQ (Linear Integrated Query) Query Expressions.

Every Query expression of LINQ will have 3 different parts. They are:

•Creating and specifying data source.
•Defining required query expression.
•Execution of the Query.


-Partial Methods:
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.


-Nullable Type
A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.

Use the ?? operator to assign a default value that will be applied when a nullable type whose current value is null is  assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;

-Covariance and Contravariance
Covariance and contravariance provide a degree of flexibility when matching method signatures with delegate types. Covariance  permits a method to have a more derived return type than what is defined in the delegate. Contravariance permits a method  with parameter types that are less derived than in the delegate type.

With contravariance, you can now use one event handler in places where, previously, you would have had to use separate  handlers




No comments: