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:

Comments

# re: C# 2.0 is coming

Tuesday, December 02, 2003 8:31 AM by bart

Hi Bart

I played with generics and it's really cool, if your intresed check out my blog (http://weblogs.asp.net/jan/posts/40139.aspx, http://weblogs.asp.net/jan/posts/38949.aspx). Another great feature of .NET 2.0 is ObjectSpaces (in my opinion). I'm just getting started with it, but it looks very cool.

cu
Jan

# re: C# 2.0 is coming

Friday, January 07, 2005 11:42 AM by bart

It's 2005 now, and still no C# 2.0

# re: C# 2.0 is coming

Friday, January 07, 2005 5:59 PM by bart

The C# 2.0 language specification is already complete for quite some time now, just like the compiler and the basic framework. The majority of the work is now focused on the tools, which you can except in mid-2005 together with the SQL Server 2005 product. I agree it takes time, but it's definitely worth the wait and quality, security are the primary focuses, not time schedules.

# re: C# 2.0 is coming

Friday, September 23, 2005 7:33 PM by bart

What happen to the collection class in generic? It was there in the VS2005 beta 1.

I am trying to convert my codes which uses collectionBase to use Generic collection.

Thx,
Paul