How to make C# 3 even better...

time to read 5 min | 878 words

After getting so excited about the things that are going to be in C# 3, let's see the things that I would like to see coming:

Extentions method for properties and operators - Operators are super important because this would be the only way to add operators to interfaces. Extentions properties are a must have if we have extentions methods. The spec mentions that they are considered, I certainly think that they should go in.

Compiler time reflection information - By that I mean a way to explicitly get reflection information where it's known. For instance, I may want to pass a property info to a method, right now I've to use strings to do it. The things that I would like to see are: methodInfo(), propertyInfo(), eventInfo(), fieldInfo(), parameterInfo(). All of them as language constructs exactly like typeof(), I can see that this would be a problem with overloaded methods, though.

The anonymous type is depedant on the properties orderring - Does this makes sense?

(new { Str = "a", Int = 1}).GetType() != (new { Int = 1, Str = "a"}).GetType();

This is just silly. What is the reason you would want  two types with the same number / type of properties to be different? This is inconsistent with the rest of the language.

Type inferencing beyond the local scope - I want to be to do the following:

public var Method() { return "something"; } 

This have a lot of implications, but it's a great productivity saver and it allows a lot of nice tricks. Some of the problems I can think of with this is what happens when you've anonymous type as a return type:

public var Method() { return new { Str = "Something" }; } 
  • What is the return type of the method?
  • How would I know from outside the method?
  • How would I know what the type is outside of the assembly?

I can see two possible way to do that:

  • Allow only static types in non-internal API, this is the best choice, I think.
  • Only allow var return types for internal methods, this is not a favoriate of mine.

In both cases, I think that it's important to allow anonymous types as a return type from a method inside a single assembly. It's a very good way to move a lot of information from one method to the other in a type safe way.

Another problem is accidently changing the return type of a method with var return type. I think that this would be cought very quickly, as it would cause immediate breakage in other places, so it's not such a big problem.

One thing to consider is situation like this:

public var Method(int i) 

      if (i%2==0) 
            return new object(); 
      else 
            return "something"

What would be the return type of the method?

  • error if they are not exactly the same type.
  • revert to the most basic type.

This leads me to another question, why are all the type inferencing insisting on keeping the same type?

var array = new []{1,"something"null}; 

The array should be object[] , and the above should not cause an error. The idea is to go to the common base class (or object, of course), and it's been successfully implemented in other languages.