Working with the New Features of C# 4.0

Leverage the new features of C# 4.0 to add flexibility to your code

Joydip Kanjilal

November 29, 2009

4 Min Read
ITPro Today logo

C# 4.0 comes in with a lot of new features and enhancements. This article takes a look at the new enhancements in the C# language in its 4.0 release and how they can be implemented in your applications.

Prerequisites
Visual Studio 2010 and .NET Framework 4.0 mark the next generation of application software development tools from Microsoft. Note that C# 4.0 is included as part of .NET Framework 4.0. To execute the code examples discussed here, you should have Visual Studio 2010 Beta 1 or Beta 2 installed in your system.

OK, so what's new? Here are the new features in C# 4.0:

  • Dynamic typing and lookup

  • Named and Optional Parameters

  • Enhanced COM Interop features

  • Variance

Added to this, you have a nice new enhancement in .NET Framework 3.5 and 4.0: PLINQ. If you are using .NET Framework 3.5, you need to download the Parallel Extensions Library. If you are using .NET Framework 4.0, you need not download and install any separate component to use PLINQ.

Parallel LINQ (PLINQ) is a concurrency execution engine for executing Language-Integrated Query (LINQ) queries. PLINQ is actually a part of the Parallel Extensions library (previously known as ParallelFramework Extensions PFX). The MSDN article, "Parallel LINQ: Running Queries On Multi-Core Processors," states: "PLINQ is a query execution engine that accepts any LINQ-to-Objects or LINQ-to-XML query and automatically utilizes multiple processors or cores for execution when they are available." .NET Framework 4.0 is actually a part of Visual Studio 2010.

Dynamic Lookup
The dynamic typing feature in C# 4.0 eliminates the need to bother about the type of the returning objects in an assignment statement; the runtime would take care of it now. Support for Dynamic Typing in C# 4.0 is actually provided by the runtime. You now have the Dynamic Language Runtime (DLR) in .NET Framework 4.0 that allows you to have static and dynamic typed languages to reside side by side. The DLR provides a set of services that include:

  • Dynamic Method Dispatch

  • Dynamic Code Generation

  • Hosting API

The Dynamic keyword is a great new feature in C# 4.0. It allows you to call C# objects without having to use the Reflection API. You can create a type that allows you to attach new methods at runtime and invoke those methods using the dynamic dispatch feature.

Named and Optional Parameters
Named and Optional Parameters are two of the most interesting new enhancements to the language. Ever thought of calling a method with without having to pass all values? Yes, you can do it now, thanks C# 4.0!

You can now use C# 4.0's optional parameters feature to set default values to the parameters of a method. Here's an example:

public static int Sum(int a = 5, int b = 10)        \{            return (a + b);        \}

Here is how you can now call the Sum() method in your application's code:

int result = Sum();

 

Refer to the previous code snippet. As you haven't passed any values, the default values would be considered and a resultant integer value of 15 would be assigned to the variable result.

You can also call the Sum() method and pass values to one or more of its parameters explicitly. See the following example:

 

int result = Sum(10,20); //The variable "result" would now have a value of 30 stored inside it.

It is also possible to pass values to the parameters of a method using named arguments. Here is an example:

int result = Sum(a:10,b:20);

 

Support for Variance
Variance is a great new feature in C# 4.0. You can now specify in and out parameters on generic types as well. The in parameter relates to "input only," and the out parameter relates to "output only." You now have support for covariance and contravariance in C# 4.0—both being the forms of variance.

Support for Primary Interop
The support for variance makes working with Microsoft Office applications and other Primary Interop Assemblies much easier in C# 4.0. In C# 4.0 you can make calls to COM methods and Interops seamlessly, with much less code. Here is an example that shows how you can open a Microsoft Word Application in C# 4.0:

Word.Application myWordApplication = new Word.Application() \{Visible = true\};myWordApplication.Documents.Open(@"C:\aspnetPRO.docx", ReadOnly: true);

Similarly, here's how you can now work with Microsoft Excel documents in C# 4.0:

var myExcelApplication = new Excel.Application();myExcelApplication.Visible = true;dynamic myExcelWorkBook = myExcelApplication.Workbooks.Add();Excel.Worksheet wkSheetData = excelWorkBook.ActiveSheet;myExcelWorkBook.SaveAs("C:\aspnetPRO.xls", AccessMode: Excel.XlSaveAsAccessMode.xlShared);

Notice that in the previous two code snippets, there aren't any missing values and casts required—things have now become much simpler.

Suggested Readings
http://www.devx.com/dotnet/Article/42262/0
http://www.devx.com/dotnet/Article/42590/1954
http://msdn.microsoft.com/en-us/vcsharp/dd819407.aspx
http://channel9.msdn.com/tags/CSharp%204.0/
http://www.microsoft.com/downloads/details.aspx?FamilyID=752CB725-969B-4732-A383-ED5740F02E93&displaylang=en

Summary
In this article, we've looked at the new features and enhancements of C# 4.0 and how you can leverage those features in your applications. You can learn more about these features in my upcoming book Visual Studio 2010 and .NET 4 Six-in-One.

Joydip Kanjilal is a Microsoft MVP in ASP.NET. He has more than 12 years of industry experience in IT with more than six years in Microsoft .NET and its related technologies. Joydip blogs at http://aspadvice.com/blogs/joydip.

Read more about:

Microsoft
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like