C# 5.0 wish list

I've been using C# as my primary programming language for about, oh 6 years now. I love it but I wouldn't say it's perfect. The C# language is lacking a lot of features that I've found to be huge productivity boosts in other languages.


Written on Wednesday, October 20, 2010 by

Let's get right to it. Extension Properties: This is first on this list because it was the first feature I thought of. Extension properties aren't something I'm dying for but they would be a nice feature to have.

public static class StringExtensions 
{
    public static int LengthWithNoSpaces 
    {
        get 
        {
            return this.Replace(  " ", "" ).Length;
        }     
    }
}

Generics in Attributes: I'm probably alone in wanting the ability to use generics in attribute classes but I can think of a few cases where this would make code more readable. e.g.

[ExpectedException<InvalidOperationException>()]

Constructor Inheritance: I'm still not sure why this isn't in c# already. I hate writing constructors that just pass arguments to base constructors.

public class A 
{
     private int number = 0;
     public A() 
     {
         number = 10;
     }
     public A(int num) 
     {
         number = num;
     }
}

public class B : A 
{
         
}

var test = new B(10); //this doesn't work!

// to get this to work you need to do:
public class C : A
{
    public C (int num) : base(num) {} // really?!?!?!
}

Nested inner classes ala Java: This is one syntax feature I really love about Java. I use this all the time when I work on Android applications. It just feels right to override virtual members inside inline object instantiations.

public class A
{
     public virtual void Test()
     {

     }
}

var ab = new A() {
    Test = () => MessageBox.Show("Test!");
};

But why stop here, why not go all in with...

JavaScript-style handling of dynamics: (Yeah, I know this won't be happening soon if ever but I can dream, right?)

var x = new dynamic {
    Test = () => { return this.SomeNumber * 2; },
    SomeNumber = 11
};

// this would work as long as return type was still int
x.Test = () => { return 11; }; 

// this would fail
x.Test = () => { return "hi"; }; 
    
// y would == 11
var y = x["Test"](); 
    
// able to add new properties like javascript
x["NewProp"] = "SomeValue"; 

So that's my list (for now) of what I would like to see in vnext of C#, what's on your wish list for C# 5.0?

Back

Let's talk about this

 


The opinions expressed herein are my own and do not represent my employer's view in any way.


Creative Commons License