Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
C# Features From 3.0 to 6.0
WhatsApp
Rakesh Dabde
9y
33.1k
0
5
300
Article
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.
public
void
Process(
string
data,
bool
IsAllowed =
false
, ArrayList data =
null
)
{
}
The following statements are valid:
Process(“Rakesh”);
Process(“Rakesh”,
true
);
Process(“Rakesh”,
true
,list); (where list
is
the
object
of List)
Named Parameters:
Process(“Rakesh”,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”, 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:
Overview of C# 3.0
C# 4.0's New Features Explained
An Introduction to New Features in C# 5.0
List of All New Features in C# 6.0: Part 1
List of All New Features in C# 6.0: Part 2
C#
C# Features
Features of C#
Learn C#
what is new in C#
Up Next
Ebook Download
View all
Programming C# for Beginners
Read by 149.3k people
Download Now!
Learn
View all
Membership not found