Advertisment

New features in C# 4.0

author-image
CIOL Bureau
Updated On
New Update

BANGALORE, INDIA: This article provides you a head start to the new features in C# 4.0. It also discusses the new features and enhancements to CLR in Microsoft .NET Framework 4.0.

Advertisment

The Pre-Requisites

It should be noted that CLR 4.0 and C# 4.0 ships with Visual Studio 2010. To work with C# 4.0, you should have Visual Studio 2010 installed in your system. As of this writing, Visual Studio 2010 RC has been released. You can download a copy of Visual Studio 2010 RC from this link: http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx

What are the new features in CLR 4.0?

Advertisment

The Dynamic Language Runtime Environment (DLR) sits on top of the CLR's managed environment and provides you support for dynamic languages from within the managed environment. So, you can now use dynamically typed languages such as Python, Ruby, and JavaScript from within the managed environment and these can languages can now reside alongside the statically typed .NET languages targeted at the CLR like, C#, Managed C++, and Visual Basic. The components of the DLR include: a .NET language integration layer, a set of runtime code components, and language binders. The DLR provides the following set of services:

* Support for Dynamic Method Dispatch

* Support for Dynamic Code Generation

* Support for a Hosting API

C# Language Improvements

Advertisment

The four major features included as part of C# 4.0 are:

1.Support for Dynamic Typing and Lookup

2.Support for Named and Optional Parameters

3.Support for Enhanced COM Interop features

4.Support for Variance

Support for Dynamic Typing and Lookup

Advertisment

Dynamic Lookup is a new feature in C# 4.0 that enables you to eliminate the overhead of casting objects when assigning values to them returned from method calls or expressions. This support is provided through the usage of the dynamic keyword in C# 4.0. You can use the dynamic keyword in C# 4.0 to create objects even when you aren't aware of the type of the object at compile time.

 

Support for Named and Optional Parameters

Advertisment

Support for Optional parameters, default values and named parameters are new interesting features added to C# 4.0. You now no longer need to bother about your parameters in method calls - they have now been made much simpler. Consider the following method:

public static int AddValues(int x = 5, int y = 10)

{

  return (x + y);

}

You can now call this method AddValues() as simply as the following call shows you to:

Advertisment

int myValue = AddValues(); //The myValue integer variable would now have a value of 15.

Refer to the code snippet above. As you can see, the default values assigned to the parameters in the AddValues() method are 5 and 10. So, even if you don't pass any value when invoking the method, these values would be considered.

Enhanced COM Interop Features

Advertisment

C# 4.0 now allows you to have better flexibility in writing COM interoperable code - it is now much easier to call COM objects from your .NET code. Refer to the following code sample that shows how you can open a Microsoft Word Document in C#.

using Word = Microsoft.Office.Interop.Word;

namespace PCQ

{

    class Program

    {

        static void Main(string<> args)

        {

            Word.Application wordApplication = new Word.Application() { Visible = true };

            object missingValue = System.Reflection.Missing.Value;

            object readOnlyValue = true;

            object fileName = @"E:\\PCQ.docx";

            wordApplication.Documents.Open(ref fileName, ref missingValue, ref readOnlyValue,

                ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,

                ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,ref missingValue);

        }

    }

}

 

Here is an example that illustrates how you can open a Microsoft Word Document in C# 4.0:

using Word = Microsoft.Office.Interop.Word;

namespace PCQ

{

    class Program

    {

        static void Main(string<> args)

        {

            Word.Application wordApplication = new Word.Application() {Visible = true};  

            wordApplication.Documents.Open(@"E:\\PCQ.docx", ReadOnly: true);

        }

    }

}

Now your code is much simpler, isn't it?

Support for Variance

C# 4.0 allows you to specify in and out parameters on the generic types. There are two ways in which you can have support for variance in C# 4.0 - covariance and contravariance. Bill Wagner says: "A return value or parameter is invariant if you must use the exact match of the formal type name. A parameter is covariant if you can use a more derived type as a substitute for the formal parameter type. A return value is contravariant if you can assign the return type to a variable of a less derived type than the formal parameter." Reference: http://reddevnews.com/articles/2009/05/01/generic-covariance-and-contravariance-in-c-40.aspx

Summary

This article has had a look at the new features and enhancements in CLR 4.0 and C# 4.0. It was an attempt to provide you a head start to the new features and enhancements in C# 4.0. You can learn more about these features in my upcoming book Visual Studio 2010 and .NET 4 Six-in-One. Here is the link to this book at Amazon: http://www.amazon.com/Visual-Studio-2010-NET-Six/dp/0470499486/ 

Author Biography

Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.NET. He has over 12 years of industry experience in IT with more than 6 years in Microsoft .NET and its related technologies. He was selected as MSDN Featured Developer of the Fortnight (MSDN) a number of times and also as Community Credit Winner at www.community-credit.com several times. Joydip has authored the following books:-

ASP.NET 4.0 Programming (Mc-Graw Hill Publishing)

Entity Framework Tutorial (Packt Publishing)

Pro Sync Framework (APRESS)

Sams Teach Yourself ASP.NET Ajax in 24 Hours (Sams Publishing)

ASP.NET Data Presentation Controls Essentials (Packt Publishing)

Joydip has also authored more than 200 articles for some of the most reputable sites like, www.asptoday.com, www.devx.com, www.aspalliance.com, www.aspnetpro.com, www.sql-server-performance.com, www.sswug.com, etc. A lot of these articles have been selected at www.asp.net - Microsoft’s Official Site on ASP.NET. Joydip is currently working as an independent software consultant and author. He has years of experience in designing and architecting solutions for various domains. His technical strengths include, C, C++, VC++, Java, C#, Microsoft .NET, Ajax, WCF, REST, SOA, Design Patterns, SQL Server, Operating Systems and Computer Architecture.

Blog: http://aspadvice.com/blogs/joydip

e-MailID: joydipkanjilal@yahoo.com

tech-news