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
- }
- }
C# 5.0 features
- Async (Asynchronous methods):
C# 5.0 Async feature introduces two keywords async and await which allows you to write asynchronous code more easily. Async and await are the code markers which mark code positions from where control should resume after a thread (task) completes.
Async and await must be used in a pair.
E.g.
- Public static Async void Method()
- {
- await Task.Run(new Action(LongTask));
- }
- Caller Information:
Caller information can help us in tracing, debugging and creating diagnose tools. It will help us to avoid duplicate codes which are generally invoked in many methods for same purpose, such as logging and tracing.
- CallerFilePathAttribute
- CallerLineNumberAttribute
- CallerMemberNameAttribute
C# 6.0 features
- Using Static:
This is the new concept of C# 6.0. No need to write Console.Writeline(“Ritesh”) or Console.Write().
Just add using statement at top and use static methods directly.- Using System.Console;
- class Program
- {
- static void Main(string[] args)
- {
- WriteLine(“Ritesh”);
- ReadLine();
- }
- }
- Auto Property Initializer:
This is a concept to set the value of a property during property declaration. We can set the default value of a readonly property, it means a property that only has a {get;} attribute.
E.g.- Public Class Employee
- {
- Public string Name {get;set;}=”Suresh”;
- Public int Age {get;set;}=25;
- }
- Dictionary Initializer:
We can directly initialize a value of a key in a Dictionary Collection with it, either the key in the collection would be a string data type or any other data type.
E.g.- Dictionary < int, string > dicInt = new Dictionary < int, string > ()
- {
- [1] = ”Rakesh”, [2] = ”Amar”, [3] = ”Nilesh”
- };
- Exception Filters:
Exception filters allows us to specify a condition in Catch block. So if the condition will return true then the catch block is executed only if the condition is satisfied.
E.g.- Catch(Exception ex) if(val==0)
- {
- Console.writeline(“Divide by zero exception”);
- }
- Easily format strings using String interpolation:
To easily format a string value in C# 6.0 without any string.Format() method we can write a format for a string. It's a very useful and time consuming process to define multiple string values by “\{ variable }”.
E.g.- String Output= “\{FirstName} - \ {LastName}”;
- Paramterless Constructors in Struct
We can create a parameterless constructor in a struct explicitly in Visual Studio 2015.- Public struct Student
- {
- int rollno;
- string Name;
- public Student()
- {
- Rollno = 0;
- Name = string.empty;
- }
- }
References:
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..