C# 5.0 came out in August 2012 with .NET Framework 4.5 and Visual Studio 2012. Given my choice in programming languages, I’ll generally pick C# over VB.NET or any other language, assuming it’s applicable. So, overall I like it a lot. However, there’s always room for improvement. Here are some nifty features that I’d like to see in a future version of C#.


Null Safe Objects

Object-oriented programming is great; typically using objects makes life easier. But there’s one situation where I often run into issues: when objects might be null. For example, say I have a function that returns the length of a string.

Now, what happens when myString is null? Boom: NullReferenceException. A null string has no length, so really I want it to return zero. So, I have to add logic to deal with the null string situation. I can do it in the same line using the ternary operator.

What I’d rather have, though, is for the language to automatically collapse that null value down instead of throwing an exception. It follows that a property of a null object should simply return null. When s is null, it follows that s.AnyDefinedProperty should also be null. If that were the case, I could simply do this and get the expected result.


With Block

This is one of those rare useful feature VB.NET has but C# does not. In VB.NET you can save yourself constantly retyping of a object name by enclosing it in a with block. I find this keeps my code nicely grouped and saves some repetition. Why not add it to C#? We can already effectively do it during object initialization using the new keyword, why not let us do it at any time?


Var Expansion

Ever feel like you’re being redundant when you type BlahObject x = new BlahObject? I do. Was it really necessary to specify Blah twice?? The var keyword lets C# implicitly type your variable, and avoid being overly redundant. I find this saves time and makes code easier to read when the type is obvious during declaration.

Full disclosure: var was not really intended for this type of use. It’s there because there are stituations where it’s impossible to explicitly type your variables at design time. Sometimes, in code you may not know what type you’re getting back until compile time, so var is used to deal with that anonymous type coming back. However, I’ve come to love using var just to save time, space, and typing when I’m my type is obvious from the assignment.

What I’d like to see is expanded use of var for implict typing. Currently, you can only use var for local variables, not properties or universally throughout the language.


What Else?

I want the world. I want the whole world. I want to lock it all up in my pocket. It’s my bar of chocolate. Give it to me! NOW!!

How about you, what golden goose feature do you want in your favorite programming language?


Like this post? Share it!