My C# 5.0/CLR 5 Wish List

August 22, 2010

tags: , , , ,
11 comments

C# 5.0 is probably in the works (for sure). A while back I blogged about features I’d like to see in C# 4.0. I didn’t get any of my wishes (well, maybe I got one wish in an unexpected way, more on that later). Here’s a list of features I’d love to see in the next version of the C# language and the CLR. It’s not yet Christmas, but maybe Microsoft’s Santa needs some time to make these wishes come true:

operator constraints

This is something I wished for with C# 4.0, but didn’t get. Well, I actually did get it, but in an unexpected way. What I wanted is to used operators in a generic method:

public static T Sum<T>(params T[] values) {

   T sum = values[0];

   for(int i = 1; i < values.Length; i++)

      sum += values[i];

   return sum;

}

 

Unfortunately, this does not compile, as the compiler and CLR can’t be sure all Ts passed to the method have an addition operator. I would have wanted to add a constraint:

public static T Sum<T>(params T[] values) where T : operator+ {

 

However, currently no such constraint is supported. One possible reason is that operator overloading does not have to be implemented by a CLS compliant .NET language. On the other hand, all major languages support it, so maybe it should be permitted.

This actually can be done with the C# 4.0 dynamic keyword (without the need for generics). This is actually more flexible, because one can provide different types for addition (e.g. a Vector and a Point). Still, the runtime implications may be significant, so I want this constraint to be possible!

public static dynamic Sum(params dynamic[] values) {

   dynamic sum = values[0];

   for(int i = 1; i < values.Length; i++)

      sum += values[i];

   return sum;

}

 

Constructor inheritance

As we all know, constructors are not inherited with the usual inheritance in C#. I think that is unfortunate, as sometimes a class may have many constructors that one must duplicate to get a basic working class. The canonical example is an Exception derived class that must support at least 4 constructors. Perhaps there should be a way to inherit constructors (as is possible in C++0x, the new standard of C++).

Possible syntax for this could be a simple attribute that would make the compiler inherit all constructors and call the base class constructors blindly, or inheritance of specific constructors that could specified like so (without any need to introduce new keywords):

class A {

   public A() { … }

   public A(int z) { … }

   public A(int z, string s) { … }

}

 

[InheritConstructors]

class B : A {

}

 

class C : A {

   using base(int);           // inherited constructor

   using base(int, string);   // inherited constructor

}

 

var working everywhere

Currently, the var keyword only works with local variables. This is unfortunate – I wish it would work with fields as well:

class Customer {

   private var _data = new Dictionary<string, int>();

   private var _name = string.Empty;

   private var _age = 20;

}

 

The last two examples are definitely an abuse of that feature, but it should be helpful in the first case. Although var was really created for anonymous types, I think its type inference capability alone is worth it.

 

delegate equivalence

Delegates types are synthesized by the compiler, deriving from System.MulticastDelegate, providing an appropriate Invoke method, etc. A delegate essentially defines a prototype, that can be declared in infinite ways. For example, the System.Action<object> is logically equivalent to System.Threading.WaitCallback, as both accept an object and return void.

However, any attempt to cast one delegate type to another with the same signature fails. Why should that be so? Since the compiler is the only entity generating those delegates, it (and the CLR) should be smart enough to allow conversion, as they are semantically the same.

The Func<> generic delegate is an attempt to reduce the number of delegates defined out there, a superset of delegates such as Predicate<>, but I believe that’s not enough. Delegate equivalence should help.

 

Const methods

This is bound to be heated. The const method issue has been probably beaten to death more than once, but still, I think the idea of const methods (as in C++) and their use through const objects is important, today more than ever, with the rising of functional languages and the benefits of immutability.

I believe the .NET developer community is mature enough to embrace it and love it.

 

This is my list so far, but I reserve the right to add more wishes as Christmas approaches…

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

11 comments

  1. igor609August 22, 2010 ב 19:11

    I’d like the meta programming, a Pattern matching.
    I’d like a language such as the Nemerle language (C# 10 maybe).

    nemerle.org

    Reply
  2. Lior IsraelAugust 24, 2010 ב 11:48

    As we say without a past has no future
    It already existed in Ada language 🙂
    Lior

    Reply
  3. SpartacoNovember 4, 2010 ב 16:44

    I would like to see new keywords or well known interfaces that the compiler understand and uses to customize it’s parser process. This way the language could be extended far away.

    For example I could write an extension that extend the parser and allows new keywords. This extension could just return to the compiler ETv2 as the implementing logic.

    Reply
  4. momo-erikaDecember 23, 2010 ב 05:52

    很好,很强大,这样下去就接近C++ Template了

    Reply
  5. OriginalDaemonMay 17, 2011 ב 16:09

    I would very much like to have a more straight forward way of assigning a properties Get and Set routines to a delegate. There are some ways of doing it and you can always write a Get and Set function and pass that, but I would really like a way to simply stick the Get and Set straight into a delegate.

    Reply
  6. monomanMay 18, 2011 ב 18:08

    Be able to catch stack overflow exceptions from other threads.

    Reply
  7. Richard LeeMay 27, 2011 ב 19:01

    I would like some proper support for devices such as scannersand web cams. M$ movbes from WIA to DXShow to WMD to badly supported (and hardly working) WPF controls etc – COM wrappers are not much fun, and M$ itself just ignores comments in favour of the old favouirite “Its not supported in C#” (a close second to “It behaving as desinged” – i.e. not at all and no plans to fix it). If managed code is the way forward (as we are force fed) then we need some serious embedded framework managed classes to access devices – this is not writing drivers, its basic web cam capture etc. I also see a real need for a lower level language with C# syntax – like an unmanaged C# – to replace C++ (lets face it, C++ is at pension age right now!). I can see no reason to have a switch to allow C# to be compiled as if it were C++ – with the right set of wrappers on system classes, there would be no need for C++ at all. Don’t get me wrong – I can code in both, but C++, is NEVER a pleasure to code, once we used machien Code, then Assembler, why hang on to C++ – nostalgia???

    Reply
  8. blogs.microsoft.co.ilMay 31, 2011 ב 08:57

    My c 5 0 clr 5 wish list.. Dandy 🙂

    Reply
  9. blogs.microsoft.co.ilJune 3, 2011 ב 17:40

    My c 5 0 clr 5 wish list.. OMG! 🙂

    Reply
  10. ShockedMarch 17, 2013 ב 19:24

    Var is bad enough with local variables. You want more with it!?

    Reply