The next version of the C# programming language, called C# 2.0 will ship as part of the Microsoft .NET Framework 2.0 and Visual Studio 2005 contains new language features that are designed to ease many typical developer problems. Some of these changes include generics, nullable types, iterators, static classes, anonymous methods, and partial classes.
This article will discuss the basics of these new language features and hopefully give you some insight on how they may best be used in your development efforts, kicking off with anonymous methods.
An Anonymous Admirer
For those of you familiar with Java, then you are probably already familiar with anonymous methods. Anonymous methods allow you to define a method's implementation without defining a full method. For example, if you were assigning a Click event handler to a button, you may do something similar to Listing A.
Listing A
btnFirst.Click += delegate { MessageBox.Show(-Hi there"); };
In the original C#, you would have had to define a method handler for this by creating a method that performs an operation and then assign a delegate to the event. Most event handlers that you will use though will take parameters of some sort, and you can see that the code in Listing A does not deal with the parameters, however they can easily be assigned by placing the parameter definitions in the declaration (see Listing B).
Listing B
btnSecond.Click += delegate(object senderArgs, EventArgs ea)
{
MessageBox.Show(-The components type is - +
senderArgs.GetType().Name);
};
As you can see creating anonymous methods is simple. It is especially helpful when you are dynamically creating controls and need to define the events for the newly created control, or anywhere where you need to manipulate delegates and events that have simple implementations.
Nullable Types
Do you work with databases? Chances are that you work with databases more than anything else, which would mean that at some time you have to deal with reading in a null value from a column. Most people adopt a convention for storing a null value such as -1 for an integer column or a blank string for a string column. Included in C# 2.0 is support for nullable types where a primitive data type can have a value stored in it but can also store a null value. Declaring a nullable type field/variable is the same as declaring a normal field or variable, however after the data type you place a question mark as shown in Listing C.
Listing C
int? ReportsToID;
private string ReportsToText()
{
if (ReportsToID.HasValue)
return -Employee Number - + ReportsToID.Value;
else
return -No one";
}
The field that I am using in Listing C is actually based on the Employee table from the developer-famous Northwind database where the ReportsToID column contains a reference to another employee if that employee reports to anyone, or null if that employee does not report to anyone. In the case of the ReportsToID column shown, either an integer value or the value of null can be assigned. Listing C also shows how you can access the value of the nullable type. Each nullable type has two properties, a Boolean property called HasValue to determine whether or not it contains a value and also the Value property which has the actual value.
Generics - C# 2.0's sliced bread
Generics are a feature that you will see touted everywhere from Microsoft and in all areas of publications, and with good reason. Of all the features in C# 2.0, generics will have the greatest impact on the amount of code that you write.
So what are generics? Generics allow you to define type parameters to a class and are especially handy for collections of data or operations that are performed on any form of type. For example you may have a class of operations that work on any type or another class that stores a list of objects, but in C# 1.0 to make it work you would have to code implementations of each type for the first example, or in the collection example you would have to make sure that everything was cast to an object data type and have to continuously. When you wanted to access the object you would then need to do a type comparison to see that what you entered is the correct type.
Listing D demonstrates the code for a simple list generic class that allows you to work with a list of data types indexed by a single value. I am using this example as a demonstration of accessing a list of objects which would have a primary key.
Listing D
As you can see Generics treat types as parameters to the class definitions, and to declare a generic instance you declare the generic class with the types that will be used as shown in Listing E.
Listing E
IndexedObjectFinder<Supplier, int> SupplierList;
SupplierList = new IndexedObjectFinder<Supplier, int>();
As you can see when you declare the generic variable, you declare the types that the generic instance will use. Using Listing D as an example, the IndexedObjectFinder<Supplier> will now return a Supplier class from the GetEntry method and also take a Supplier instance as a parameter in the Add method. Generics give you the flexibility to always work with the data type that you specifically want at compile time. For this example, without generics you would have to define a base class for the collection and then continually subclass in order to get the desired type functionality. Generics without a doubt will be the language feature that will impact C# developers everywhere because it has the potential to save an extraordinary amount of time.
Do you need help with .Net? 





1
sakthi - 08/08/05
please give the example
how we zoom the image in imageedit control
and also rotate the imagein imageedit control
please give solution for image edit control in c#
» Report offensive content
2
D - 08/10/06
Hi, i am trying to design a website using Visual Studios 2005...
The only problem is...it has not got an option of selecting a web form...How do I get around this problem?
D
» Report offensive content