.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

C# 3.0 New Features - Part 1

Posted By:Kumar       Posted Date: December 26, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

Below are the new features of C# 3.0

1. Implicitly Typed Local Variables

C# 3.0 introduces a new keyword called "var". Var allows you to declare a new variable, whose type is implicitly inferred from the expression used to initialize the variable.This is a new and handy keyword that'll save you some typing. Example below:

// C# 2.0

int i = 10;

string str = "Hello Techies";

// New C# 3.0 var type

var i = 5;
var str =
"Hello Techies";

The var keyword is NOT a completely new type, instead the compiler just takes a look at the right-hand side of the expression. If the right-hand side is an int, for example, the compiler will "replace" the var keyword with int. This means that the following contruction will NOT work:

// Will not work, 'cause the right-hand side of the var is unknown

var b;
b = 1556;

Some Restriction:

We cannot do the increment and decrement operation like i++ or ++i

We cannot declare the NULL value to the var variable

We cannot initialize the multiple var variables like others. Int  I, j, k

2.Anonymous Types

C# 3.0 gives you the flexibility to create an instance of a class without having to write code for the class beforehand.

new { Name = "Kumar", Gender = "Male", Age = 25 };

In the background, the C# compiler would create a class that looks as follows:

class __Anonymous1

private string _Name = "Kumar";

private string _Gender = "Male";

private int _Age = 20;

public string Name { get { return _Name; } set { _Name = value; } }

public string Gender { get { return _Gender; } set { _Gender = value; } }

public int Age { get { return _Age; } set { _Age = value; } }


Now the user have a class, but the user still need something to hold an instance of the above class. This is where the "var" keyword comes in handy; it lets you hold a statically typed instance of the above instance of the anonymous type. Here is a rather simple and easy use of an anonymous type:

var employee = new { Name = "Kumar", Gender = "Male", Age = 25 };

3.Auto Implemented Property

This property plays significant role to set and get the values. In the previous versions we used to set the values to the temporarily local variable. Now it has been reduced in the C# 3.0 version. Because, .Net compiler will handle it implicitly

Let's see the new version, Auto implemented property.

[Access-modifier] data type [Name of the property]


For example we are going to create one property for Employee.

public string employeeID {get; set ;}

public string
employeeName {get; set ;}

age {get; set ;}

In previous version we have done like this.

private string _employeeName;

public string employeeName


return _employeeName;



_employeeName = value;



Here in C# 3.0, it became auto implemented property, that means in the runtime, the .net compiler will assign the values to temp variable.

4.Object initializers

In C# 3.0 a new way to initialize objects. Example below:

// The old way
Employee emp = new Employee(); // Create object
emp.Name = "Kumar"; // Initialize property Name
emp.Gender = "Male"; // Initialize property Gender
emp.Age = 25; // Initialize property Age

// New way
Employee emp = new Employee { Name = "Kumar", Gender = "Male", Age = 25 };

This will also save some lines of code

Hope this article helps you.

Happy Learning and Coding


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend