...Technology Simplified

Friday, May 11, 2012

New Language Enhancements in C# 4.0

No comments :
• Auto-implemented properties
For accessing Private variable outside class in .Net , property is needed.
By using Auto implemented properties,there is no need to create private variables.
There is no need to write lengthy code for get, set methods.
• Object Initializer
Object initializers let you assign values to properties of an object at creation time without having to explicitly invoke a constructor.
Ex: Employee emp = new Employee{
EmpId = 111,
Ename = “aaradha”,
Salary=12000
};
• Collection Initializer
Collection initializers let you specify one or more element intializers when you initialize a collection class that implements IEnumerable.
Ex: List emplist = new List {
new Employee{ EmpId = 111, Ename = “aaradha”, Salary=12000 },
new Employee{ EmpId = 112, Ename = “Conor”, Salary=13000 },
new Employee{ EmpId = 113, Ename = “Higgins”, Salary=10000 }
};
• Implicitly typed local variables
When type of data to be stored is not Known ,then declare it as var.
Once value is assigned , cannot implicitly convert the data type.
Var in javascript is loosely typed whereas in .NET is strongly typed.
• Nullable Types and Variables
NULL is understandeable by character,string,objects but not by INT.
Suppose if database retrieves data as NULL in integer value it needs to be checked for null.
So introduced Question mark after datatype to check for null.
Ex: Int? num = null;
• Anonymous Methods
Delegates is similar to function pointer,used to store address of method / function.
Anonymous method is a method without name.
If method signature matches with delegate,then it is implemented.
Anonymous method is declared using delegate keyword.
Anonymous method is nested inside other method (main)
They get address of method at runtime.
• Lambda expressions
These are Anonymous methods with reduced syntax.
There are two types of Lambda expressions.
Expression Lambda: (params) =>
Statement Lambda: (params) => { }
Only assignment, call, increment, decrement and new object expressions can be used as statements.
• Anonymous types
Used to create properties at Run time.
Ex: var student = { Sid=1, Sname=”Declan”, Marks=200};
• Extension methods
Way of adding behaviour to the class without modifying.
Suppose we have string class which has methods for length, substring, split..etc.. If we want to find number of words in string without making any changes to existing class functionality,we go for extension methods.
Extension method should always be static and should be in static class.
And parameter in Extension method should have this keyword so that compiler understands it as extension method.
• Partial methods
Partial methods is a mechanism of declaring the methods in one place and defining the methods in some other place.
Should be inside partial class.
Partial methods are by default private.
Should always have void as data type, means it do not return any value
Ex: Partial void Add(int x, int y );
• Named Parameters and optional Parameters
Existed earlier in C++ is mplemented now in .NET 4.0
Assigns default values to parameter.
Ex: public int (int x , int y=10)
Here in this example x is mandatory where as y is optional

No comments :

Post a Comment