Tuesday, April 01, 2008

What's new in C# 3.0 ?? - Part 1

I'll be writing -in shaa Allah- a series of posts about the new features in C# 3.0.. I'll start this one with the improvements that were done on C# 2.0..

1) Auto-Implemented Properties:
How much time does the one spend writing private members in a class & writing their public properties.. too much ha? .. I hear someone saying "you can use snippets" .. ok, that will be fine yet this is still too much.. so here is one of the enhancements done in C# 3.0 which is the auto-implemented properties.. instead of writing sth like this..

class Employee
private string
public string
{ empName = value; }
{ return this.empName; }

All you need now is to use the auto-implemented properties instead..

class Employee
public string
EmployeeName { get; set; }

The question regarding accessing private members from inside the class, well.. as there's no private members (or they are hidden), you have to use the properties to access them too..

2- Object Initializers:
Sometimes we want to set a lot of properties of an object while initializing it, but yet this isn't supported in any of the class constructors..so here comes the importance of the object initializers feature.. which will be very helpful while working with win forms & many of the .net libraries in setting properties more easily..

static void Main(string[] args)
e = new
Employee () { EmployeeName = "Roaa"};

3- Collection Initializers:
The same concept as the object initializers, but the difference is setting the values of a collection, It can be used by any collection which implements IEnumerable..

static void Main(string[] args)
employees = new List<

Employee () { EmployeeName = "Roaa" };
Employee () { EmployeeName = "Someone" };
Employee () { EmployeeName = "Roaa"};

4- var - Local Variable Type Inference
For an example as the follows, we notice the redundancy of information of the type..

employeesDict = new
Dictionary<int,Employee> ()

As the matter in fact, the compiler can be intelligent enough to find out about the type of the variable when a value is specified in the assignment statement.. so here comes the use of the 'var' which makes writing the code more smoother & easier..

var employeesDict = new

but there are some restrictions on using 'var'.. as it is clear, it is for Local use only.. can't be used in return values, arguments.. etc..
Another thing, the use of 'var' doesn't affect the performance by any mean..

To be continued...


mahmoud alam said...

Employee e = new Employee () {EmployeeName = "Roaa"};

WaW, first time i see it ............

Muhammad said...

var ,
very nice but i think it may has some ambiguity .
i must know it's type if i needed to make typecasting.

Roaa Mohammed said...

That's why they restricted its usage locally only & it can't be used as function's return type nor parameters..
but you can still know a variable type from GetType() function if you needed that..
The main purpose is to support the developer with flexibility in writing his code & I think it is one of the nicest features..

Thanks Muhammad for your comment..

Mohamed Gamal El-Den said...


May be u will feel how much Var is great if u r one of foreach addicts :D

By the way, Nice Post Roaa

Ramy Mahrous said...

var; was step towards dynamic programming.

I liked your new blog style, it really beautiful and the most I like "C#de" btw, Gamal Helwan-MSP wrote a series in C# 3.0 all Egyptian MSPs share the same think :)

Roaa Mohammed said...

Thanks Ramy for your comment & feedback about the new style..
btw, you made me notice that the new style doesn't display the date on which the post was written, so Thanks :D