Saturday, November 22, 2003 1:21 PM
bart
C# 2.0 is coming
Whidbey will not only be the release of ASP.NET v2.0, the release of major improvements in the field of Web Services, and so on, it will be the release of C# 2.0 as well. C# 2.0 contains a lot of great new features which were only available to C++ developers today:
- Generics: think of collections today, such as a queue, stack, list, map, etc. To make sure such collections can contain any possible element, all the methods (such as Pop and Push for a queue) work with the object type. However, this causes a lot of overhead since you'll need to cast retrieved elements back to their original type. Generics (which are much like the C++ templates) solve this problem by allowing the user of such a collection to specify the type of the elements which will be stored in the collection.
Today:
Stack stack = new Stack();
stack.Push("hello");
string s = (string) stack.Pop();
Tomorrow:
Stack<string> stack = new Stack<string>();
stack.Push("hello");
string s = stack.Pop();
To make such a generic class take a look at this little example:
public class Stack<T>
{
//...
public void Push(T item)
{
//...
}
public T Pop()
{
//...
}
}
Things such as multiple generic types are also possible (think of a LinkedList with two associated types, one as the value and one as the key):
public class LinkedList<K,T>
Make sure to read this article on MSDN: http://msdn.microsoft.com/vcsharp/default.aspx?pull=/library/en-us/dv_vstechart/html/csharp_generics.asp
Other features which have to do with generics are:
- generic constraints (allowing you to define a constraint on the 'associated types' of a generic type
- inheritance of generic types
- generic methods and generic delegates
- Iterators
- Anonymus methods/delegates
- Partial classes
You can find the specifications in this document on http://download.microsoft.com/download/8/1/6/81682478-4018-48fe-9e5e-f87a44af3db9/SpecificationVer2.doc. As sneak preview on C# 2.0 in Whidbey can be downloaded here: http://download.microsoft.com/download/2/d/2/2d2b339a-95b9-4f0e-a761-3c62c043ff5d/CSharpSneakPreview.doc
Del.icio.us |
Digg It |
Technorati |
Blinklist |
Furl |
reddit |
DotNetKicks
Filed under: Microsoft