C# Language 4.0

With Visual Studio 2010 we will see .Net Framework 4.0 and C# Language 4.0. In this blog post I have tried to list out features of C# Language 4.0

C# Language History:

Microsoft Launched .Net Platform and C# programming language in year 2000 from there C# become most popular language among developers.Initial version of C# is type safe,object oriented, simple programming language.
Version 2.0: Support for generics, anonymous methods, iterators, partial types and nullable types
Version 3.0: In version 3.0 more emphasis on LINQ(Language integrated query) along with that other important things are Implicitly Typed Local Variable,Extension method, Lambda expressions, Object and collection initializers, Anonymous types,Implicitly typed arrays and query expression and expression trees.

Version 4.0: Following are the features of C# version 4.0

Dynamically Typed Objects:

If class is not statically typed it may be written in COM,Ruby,Python or JavaScript. Or the class is dot net object but we don't know which type it is at compile time.In this case we will need dynamically typed objects then given type will get resolved at run time and c# compiler will not through any error at compile time.

Example:
We want to invoke Add method on Calculator class at run time we will possibly write code below
object calc = GetCalculator();
Type type = calc.GetType();
object result = type.InvokeMember("Add", BindingFlags.InvokeMethod,null,new object[]{1000,2000});
int sum = Convert.ToInt32(result);
With C# 4.0 this code will become
dynamic calc = GetCalculator();
int sum = calc.Add(1000,2000);
Optional and Named Parameters:

Optional and named parameters feature of c# 4.0 allow us to pass parameters to methods optionally we can specify the default value for optional parameters in method signature and it also allow us pass named parameter specifying name of the parameter.

Example:
We commonly use overloading of method we will possibly write code below
public int OverloadedMethod(sting requiredParam) { }
public int OverloadedMethod(sting requiredParam, string optionalParam1) { }
public int OverloadedMethod(sting requiredParam, string optionalParam1, string optionalParam2) { }
public int OverloadedMethod(sting requiredParam, string optionalParam1, string optionalParam2, string optionalParam3) { }
With C# 4.0 above method will get re factored as
public int OverloadedMethod(sting requiredParam
, string optionalParam1="SomeDefaultValue"
, string optionalParam2=null
, string optionalParam3="SomeDefaultValue")
To call above method simply use
OverloadedMethod("value1");
//Overloded method
OverloadedMethod("value1","Value2");
//Using named Parameters.
OverloadedMethod("value1",optionalParam3: "Value3");
Improved COM Interoperability:

With optional and named parameters the C# 4.0 provides significant improvement in COM interoperability.You can omit ref modifier while performing COM interoperability, although the use of ref modifier is required when not performing COM interoperability. With the previous version it is necessary to ship a Primary Interop Assembly(PIA) with your managed code.This is not necessary when using C#4.0 because compiler will inject all the types which you are using directly in the managed assembly.

Example:
While performing COM interop we possibly write
object fileName = "test.docx";
object missing = System.Reflection.Missing.Value;
doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
With c# 4.0 above code will become
doc.SaveAs("test.docx") ;
Safe Co-Variance and Contra-Variance:

Generics in C# 4.0 support safe Co-Variance and Contra-Variance through the use of in and out keywords.

Example:
Co-Variant type is signaled using out keyword.
IEnumerable<out T>
Contra-Variant is signaled using in keyword.
IComparer<in T>

1 comment: