Saturday, June 08, 2013

LINQ 06- Automatic type inferencing

Automatic type inferencing allows you to declare variables without specifying the type before the variable name.

You don’t have to specify the exact type when initializing variable with a type specific value

Example:

Instead of

int i = 10;

you can use

var i = 10;

 

Same can be used for objects to

var e = new Employee();

In case you want a base class type to be used for the variable, you can use casting.

var p = (new Employee()) as Person;

 

Note:

    • This can only be used inside a code block (methods, property get set blocks, etc)

No comments:

Post a Comment