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…