Tuesday, September 1, 2009

C# Features


C# 2.0
· Generics
· Nullable types
· Iterators
· Partial class definitions
· Anonymous methods
· The :: operator
· static classes
· Covariance and contravariance
· Fixed-size buffers
· Friend assemblies
· extern aliases
· Method group conversions

C# 3.5

  1. Auto Implemented Properties
  2. Local Type Inference (var keyword)
  3. Object and Collection Initializers
  4. Anonymous Types
  5. Extension Methods
  6. Lambda Expressions
  7. Introduction to LINQ to Objects
  8. LINQ Projection
  9. LINQ Where Clause
  10. LINQ From and Join
  11. LINQ Ordering and Grouping
  12. LINQ Set Operations
  13. LINQ Quantifiers



C# 4.0 Language features:
Dynamic Type
Visual C# 2010 provides support for late binding to dynamic types by introducing a new type, dynamic. This addition enables many new scenarios, including simplified access to COM APIs such as the Office Automation APIs, to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).
Visual C# 2010 introduces a new type, dynamic. The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object. At compile time, an element that is typed as dynamic is assumed to support any operation. Therefore, you do not have to be concerned about whether the object gets its value from a COM API, from a dynamic language such as IronPython, from the HTML Document Object Model (DOM), from reflection, or from somewhere else in the program. However, if the code is not valid, errors are caught at run time





Auto Implemented Properties

==========================





Auto Implemented Properties provide a short-cut to creating the verbose getter and setter methods, as well as the accompanying private fields used when creating classes.




In pastwe used to wirte get and set properties to access the private variables, As we generally store values of properties in private fields.




private int orderID;




public int OrderID {




get { return orderID; }




set {orderID = value; }




}







So if we have 100 properties then we have to write the same code for 100 times , depending upon the properties being implemented in the class.




The same thing has been reduced with the help of Auto implementing properties in C# 3.0




so the same code above can be changed as below:




public int OrderID {get; set; }




so thereis no need to store the private field like above, as that is all done implecetly through the use of this syntax




ALSO there is another short cut on top of this short cut as CODE SNIPPET




prop TAB TAB (write prop and hit TAB 2 times)




public int property { get; set;}




for readonly property




propg TAB TAB (write propg and hit TAB 2 times)




public int property { get; private set; }




Local Type Inference

==============================




Also known as implicitly typed local variables




This introduces the var keyword to C# 3.0, and it isused basically as an easy way to create a variable without having to knowthe type. This introduces the var keyword to C# 3.0, and it isused basically as an easy way to create a variable without having to knowthe type. When we work with LINQ, many times it may not be very clear whata given linq query is returning, and so the var keyword allows us to say "we don't really care what it is, let C# figure it out."




e.g





var first =7;
Console.WriteLine("First variable type: {0}",first.GetType());



O/P System.Int32 so it seems that first is of type Int32




var second = 7;
Console.WriteLine("Second variable type: {0}", second.GetType());



O/P System.Int32 so it seems that var is same as object but that is not TRUE
it is boxed up and unboxed whenever its referenced.



to prove that we do



int third=second //error as cann't do this implectly but can be done explectly



int third=first;



Console.WriteLine("Third variable type: {0}", third.GetType());



so this proves its not same as boxing and unboxing



And the moral is var infers the actal type there is no boxing , and its completely type safe.

Now the things which you cann't do

  • var may only appear within a local variable declaration, so var has to be used within the context of an method.
  • you cann't use var as an input parameter for a method.
  • you cann't use it as a return type.
  • the value for the var variable has to be initalised, so that it know what type to be created for the variable.
  • you cann't assign null to a var variable.

Object and Collection Initializers

=========================

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. These work similiarly to an initialized array, where we're able to set the properties of a newly created object or multiple instances of new objects in a new collection in essentially one line of code. In someways it's similar to a constructor that populates the properties of a newlyinstance of an object. However, unline a constructor, you can pick andchoose which properties to populate in any combination.

private class Cat{

// Auto-implemented properties

public int Age { get; set; }

public string Name { get; set; }

}

static void MethodA()

{

// Object initializer

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

}

Collection initializers let you specify one or more element intializers when you initialize a collection class that implements IEnumerable. The element initializers can be a simple value, an expression or an object initializer. By using a collection initializer you do not have to specify multiple calls to the Add method of the class in your source code; the compiler adds the calls.

e.g The following collection initializer uses object initializers to initialize objects of the Cat class defined in an earlier example. Note that the individual object initializers are enclosed in braces and separated by commas.

List cats = new List

{

new Cat(){ Name="Sylvester", Age=8 },

new Cat(){ Name="Whiskers", Age=2},

new Cat() { Name="Sasha", Age=14}

null

};

You can specify null as an element in a collection initializer if the collection's Add method allows it

No comments:

Post a Comment