C# 3.0 New Features: Part I


Microsoft has introduced the C# 3.0 with many key features. It reduces the work of the developers to write many lines of code to achieve the target. It will decrease the code size as well as the additional overhead of the servers.

There are many features like Auto implemented property. Anonymous types, Partial methods, object initializes, implicitly typed local variables.

New Features

1. Auto Implemented Property

The property plays significant role to set and get the values. In the previous versions we were doing like need to set the values to the temporarily local variable. Now that work exactly reduced in the C# 3.0 version. Because it will be implicitly handled by the .Net compiler.

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 send email.

string FromID {get; set ;}
public string ToID {get; set ;}
public string Subject {get; set ;}
public string Message {get; set ;}

n previous version we have done like this.

string _FirstName;
public string FirstName
        return _FirstName;
        _FirstName = value;

Here in C# 3.0, it became auto implemented property, that means in the runtime it will put one temp variable for assign the values by the compiler.

2. Implicit Typed local variable

The new data type introduced in the C# 3.0. Normally when you store the elements in the string with the integer value we need to do the type casting. The variable that will be declared with the var keyword and it will be inferred by the compiler at the time of execution.

Total = 10 + 10;
var iTotal = 10 + 15.5;
var Name = "senthil" + "kumar";
var Name = "senthil" + 10;

Here what happens when you declare the  var Total = 10 + 10, it will do the addition.

On the other hand, when we declare this variable in the string what happens? Let's see

name = 10 + 10;

The result will be 1010. Because it will do the concatenation, until unless do the type casting. Here the expression will be inferred by the compiler.

It can be used to create the array in the similar way.

There are some restrictions to use this feature.

  • We cannot do the increment and decrement operation like i++ or ++i
  • We cannot declare the NULL value to the var variable.
  • It use be declare and initialize on the same statement in the local scope.
  • We cannot initialize the multiple var variables like others. Int  I, j, k;

3. Anonymous Types

An anonymous type is one of the new features introduced in the C# 3.0 version. It is a read only property and can be used to assign the set of names constants with the values. These values cannot be changed the outside Anonymous type.

Names = new
    FirstName = "Senthil",
    LastName = "Kumar",
    Age = "27"

It can be accessed with the following:

_FirstName = Names.FirstName;

Normally when we define in the enum, there we cannot assign any values.

It will take the order from 0 and will be incremented by one for the every constants in the enum list.

Here the value can be assigned.


So far, we have discussed few features of  C# 3.0. Let us see the other features in the article part2. Mean time if you found any mistakes or changes, please feel free to contact or post the suggestions.

Up Next
    Ebook Download
    View all
    View all