C# 3.0 features
- Var keyword:
Implicitly typed local variable. Need to initialize at the time of declaration. The data type of a ‘var’ variable will be determined when assigning values to variables. After assigning the variable value, the variable has a defined data type and cannot be replaced.
E.g.
- var abc=1;
- Auto Properties:
Auto implemented properties.
E.g.
- Public stirng Name {get; set;}
- Anonymous Types:
An anonymous type is a simple class created on the fly to store a set of values. To create an anonymous type, you use the new keyword followed by an object initializer, specifying the properties and values the type will contain.
E.g.
- var EmpName= new {Name=”FirstName”, Age=20};
- Extension Methods:
Extension methods allow an existing type to be extended with new methods, without altering the definition of the original type. An extension method is a static method of a static class, where the “this” modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.
E.g.
- public static class ABC
- {
- public static int ChangeData(this string str)
- {
- return int.Parse(str);
- }
- }
- Lambda Expressions:
A lambda expression is an unnamed method written in place of a delegate instance.
A lambda expression has the following form:
(parameters) => expression-or-statement-block
E.g.
- X=> x*x;
C# 4.0 features
- Named and Optional Parameters:
Optional Parameters:
Optional parameters allows you to give a method parameter a default value so that you do not have to specify it every time you call the method. This comes in handy when you have overloaded methods that are chained together.
E.g.
The following statements are valid:- public void Process( string data, bool IsAllowed = false, ArrayList data = null )
- {
- }
Named Parameters:- Process(“Rakesh”);
- Process(“Rakesh”, true);
- Process(“Rakesh”,true,list); (where list is the object of List)
Above statement is invalid as we have omitted 2nd bool parameter. But we can use the following method using named parameters feature.- Process(“Rakesh”,list);
- Process(“Rakesh”, data:list);
- Dynamic:
Dynamic are dynamically typed variables. No need to initialize the dynamic variables at time of declaration. Dynamic variables can be used to create properties and return values from a function. The following syntax is valid using dynamic.
- dynamic data=100;
- data=”Welcome to C#”;
- Co-Variance and Contra-Variance:
Co-Variance:
Co- variance preserves assignment compatibility between parent and child during dynamic polymorphism.
E.g.
The following syntax is possible in C# 4.0:
- Public class Animal
- {}
- Public class Cat: Animal
- {}
- Public class Program
- {
- Public static void main()
- {
- IEnumerable < Animal > animals = new List < Cat > (); // not valid in earlier C# versions
- }
- }
Rohan ThitePosted Mar 1, 2016, 6:33 AM
nice one Rakesh Sir !!
Jithil JohnPosted Feb 16, 2016, 2:05 AM
Great
Anu VPosted Jan 14, 2016, 2:05 AM
nice
Sameer KhanPosted Jan 7, 2016, 12:45 AM
Very Good Keep it Up !!!!
Mahesh AllePosted Jan 6, 2016, 7:14 AM
Excellent...
Sridhar SharmaPosted Jan 3, 2016, 10:42 AM
Nice Share :)
Rajesh SinghPosted Dec 27, 2015, 11:42 AM
great..
Humayun Kabir MamunPosted Dec 22, 2015, 6:24 AM
Nice...
OnlyFor EducationPurposePosted Dec 21, 2015, 12:01 AM
Hats off
Antonio PelleritiPosted Dec 17, 2015, 8:31 AM
structs cannot contains explicit parameterless constructors...
Sabyasachi MishraPosted Dec 17, 2015, 3:36 AM
well explained.
Rhishikesh LathePosted Dec 16, 2015, 11:02 PM
Great. Really helpfull
sreenivasa kPosted Dec 16, 2015, 12:27 PM
really good
Pankaj Kumar ChoudharyPosted Dec 16, 2015, 11:53 AM
Nice Explain .................
Del WoodcockPosted Dec 16, 2015, 10:22 AM
Great
Raja TPosted Dec 16, 2015, 10:14 AM
Nice, Thanks for sharing
Anu VPosted Dec 16, 2015, 2:22 AM
thanx for share..