Several language extensions are introduced in C# 3.0 code-named "Orcas". These extensions are built on C# 2.0 to support functional programming style.
-
Auto-implemented Properties
-
Implicitly Typed Local Variables
-
Extension Methods
-
Lambda Expressions
-
Object Initializers
-
Collection Initializers
-
Anonymous Types
-
Partial Methods
-
Expression Trees
-
Query Expressions
Before jumping into the new features I would like to introduce the readers to the new world of functional programming in.Net. Programming styles are generally classified into two kinds.
-
Functional Programming
-
Procedural Programming
Functional programming otherwise called as declarative programming was designed specifically to support pure functional approach in problem solving. In contrast, most of the OO Programming languages like Java, C#, C++ etc., primarily support Procedural programming.
Primarily Procedural languages deal with algorithms on how the tasks are performed in problem solving. Whereas, in the Functional programming programmers define methods or functions and focus mainly on how the information is transformed.
Below sections I would be covering the major language enhancements brought in to C# 2008.
Auto-Implemented Properties
In C# 2005 defining the properties (accessors or mutators) require large typing effort even though it is used for a very simple task of encapsulation of data.
In C# 2.0 properties are declared as follows.
private int iEmpId;
private string strEmpName;
public int EmpId
{
get { return iEmpId; }
set { iEmpId = value; }
}
Samething is redfined in C#3.0 using auto-implemented properties feature as follows.
public class AutoProperties
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public int Age { get; private set; } // read-only
}
Behind the scene, compiler automatically creates private, anonymous fields that can be accessed from the above auto-implemented properties.
Implicitly typed local Variables
When a Local variable specifies var as the type, then the type of the variable will be inferred automatically by the compiler from the expression. For example:
var iAge = 33;
var strName = "Chandra";
var Expr = 5.0 * 6.0;
All the above mentioned local variables are strongly typed at the compile time and are valid Expressions. They are equivalent to
int iAge = 33;
string strName = "Chandra";
double Expr = 5.0 * 6.0;
These implicit variables come with some restrictions and they are discussed below
-
Implicit Variables are not allowed in the global scope.
-
Variables that has the value of null reference cannot be declared as type 'var'. Compile-time type cannot be null.
var x = null;
-
Declaration must include initialization expression also.
var y;
-
Initialization can include an object or collection initializer with the keyword "new".
Ex., var c = new Int32[] { 1, 2, 3 };
Following Statements will fail as there they are not "new" Expressions.
var z = { 1, 2, 3 };
var obj = Emp{ EmpID = int.Parse("1234"), EmpName = "Chandra" };
-
Cannot have auto poperties declared as var.
public var EmpID { get; set; }
-
Implicit variables can also be used in the iterative statements like "for" statement and resource-acquisition statements like "using" statement.
foreach (var a in new Int32[] { 1, 2, 3 })
Console.WriteLine(a);
Keyword 'var' was not part of default list. So for the backward compatibility if a member of type named 'var' exists in the scope, the declaration will refer to that type only. A general warning would be generated by the compiler to bring attention of the programmer.
Methods cannot return implicit types. Following code will result in compile time error.
public var BadThings()
{
return 0;
}
Extension Methods
They are static methods that can be invoked like an instance method. Static methods can be declared only in Static classes. Let's see how static methods are declared.
public static void ExportToExcel(this DataSet ds)
{
//do something...
}
Extension methods are declared by specifying the "this" keyword as the modifier in the first parameter. The ExportToExcel method is declared as an extension method for the DataSet class.
Extension methods are invoked as soon the namespace of the containing static class is included.
In the below code snippet, the extension method is declared inside
namespace Orcas.Samples
{
public static class Extensions
{
public static void ExportToExcel(this DataSet ds)
{
//do something...
}
}
}
Above method is invoked in the client program as,
using Orcas.Samples;
DataSet ds = new DataSet();
ds.ExportToExcel();
Rules governing Extension methods are
-
Instance methods should be declared only in the static classes.
-
Instance methods take precedence over extension methods
-
Extension methods imported in inner namespace declarations take precedence over extension methods imported in outer namespace declaration


Ken HPosted Feb 21, 2014, 7:48 AM
Nice article,this allows us to benefit. I hope you can continue to provide some of the articles. Thanks. :)
karthik pPosted Jan 26, 2010, 2:04 AM
sir,i'm karthik from trichy,i want to have my career in c#.net and i need your help to improve my technical knowledge,i hope you help me..email:[email protected] mobile9629663189