The C# 2.0 Language - What’s New (Part I)
Posted by: Suprotim Agarwal ,
on 3/11/2007,
in
Category C# 2.0, 3.0 and 4.0
Abstract: C# was created by Microsoft as a major part of its .NET initiative. C# is a modern, powerful, component-orientated, flexible, safe, internet friendly and an easy to use language that takes advantage of virtually any feature in this platform. Well people familiar with C# 1.1 know that. This article is a two part series. Let us explore what’s new in C# 2.0
Introducing C# 2.0
With the release of Visual Studio 2005 (formerly codenamed Whidbey), C# is back with some new innovations. The C# language has been updated to version 2.0 and comes with several language extensions which we will explore in this 2 part series article. Besides Generic types, the C# language introduces some other interesting features such as Iterators, Partial Classes and Anonymous methods, which are briefly mentioned below.
Generics
Although new to the .NET platform, this concept has been around for years. Generics is a term used to describe generic types. It greatly enhances application performance and type safety and allows you to write highly general, flexible and safe code. It permits classes, interfaces, structs, delegates and methods to be parameterized by the types of data they store and manipulate.
Iterators
Iterators specify how a for-each loop will iterate over a collection and return its internal items. They allow you to create enumerable classes with minimum coding.
Partial Classes
Partial Classes allows you to split a single type, like your class, across more than one file (.cs). They are useful if you have extremely large, unwieldy types.
Nullable Types
Nullable types allow a variable to contain a value that is undefined. This is useful when working with databases where the value returned might be null
Anonymous Methods
Anonymous Methods Are Inline Delegates. It enables you to pass a block of code as a parameter. Anonymous methods enables you to directly associate a block of code statements to a given event. You use them anywhere a delegate is used . So there is no need to define a new method.
Namespace alias qualifier
C# 2.0 adds the namespace alias qualifier, the :: operator, to provide more control over accessing namespace members.
Static Classes
With C# 2.0, now you also can declare classes as static. This signals the compiler that this class should not be instantiated and can only contains static members.
Property Accessor Accessibility
C# 2.0 allows a fine control over the accessibility of accessors and indexers within a property.
Covariance and Contravariance in Delegates
The method passed to a delegate may now have greater flexibility in its return type and parameters.
Conclusion
In this article, we explored the various new features introduced in C# 2.0. In Part two, we will explore these new features in detail.
Give me a +1 if you think it was a good article. Thanks!