Things I would like C# to have

In general C# is a great language, but being a long-time polyglot there are two things I’m really missing which I think would make the language better.

Checked exceptions

I come from a long stint in Java, which has these, and I really don’t understand the reasons for not including them in C#. As far as I know this is the answer straight from the horses’ mouth . For those that don’t know what checked exceptions are; they are exceptions that you have to either catch or throw to the next guy. Like this in java:

void foo(int x) throws SomeException()
   if (x == 42) throw new SomeException();

Any code using foo have to either catch it or declare that they also throw it. This causes an automatic documentation of what serious errors can happen in a function, since you are required to handle it. When I program in C# I just don’t know what bad things can happen in my code without looking at some other documentation than what intelliSense + resharper gives me. I find this scary, and I constantly wonder if I’m having holes in my application.

For this reason, I would really like to have this in C# in some shape or form. I don’t buy the reason of version compatibility or that people are sloppy and just go re-throwing everything. If you have that problem, your developers are at fault and you can’t design a language based on that.

Const the way it should be

Have you ever done C++? Have you ever done the const keyword the way it’s supposed to be used?

Const in C++ isn’t just a replacement for an evil #define . Oh no, it’s a whole new world once you understand it. Here’s a valid class signature in C++:

class Foo
    const Foo const * bar(const Foo const * foo) const;

In understandable terms, this it what the signature says: A method named bar takes a pointer whose location may not change and that may not be used to change whatever it points to returns a similar unchangeable pointer while not altering the class in any way.


The final const on the method is the really interesting one. It says that calling this method on the class will not change the class at all. So if you have a const instance of the class, you can all all the methods that guarantee that the class won’t change – but you can never call the non-consted ones.

Also the const* is interesting. Const in C# means I cant change what the reference points to . That is what the const before the class name stands for in C++. But the const* means I cant change the actual object at the end of my pointer.

Another awesome thing is that you can overload methods by just varying their const-ness.

Had I had this in C# I could writer safer code with less change of side effects. Also it would not really impact existing code that much. I don’t get why C# doesn’t have it. Perhaps a constref keyword to not break existing stuff? I get the feeling the real reason for not doing this is because most people doing C++ don’t understand how awesome const is. Whenever I do C++ I feel it’s almost my sacred duty to promote it’s proper use.

How about you, is there anything you miss? Please do leave a comment, I’d love to hear from you!

Tagged , ,

Leave a Reply


%d bloggers like this: