I've been playing with Code Contracts on VS2008 (http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx).
They surely are nice and provide a solid alternative to if-then-throw checks inside methods.
Nonetheless I've been hoping that they could satisfy the need that I strongly feel for non-nullable reference types.
Alas, from what I could see this doesn't seem to be the case.
This is what I understood:
Something like this will still cause issues at runtime:
MyClass a = null;
a.ToString();I still have to explicitly write checks, even if in a more concise and streamlined way.
Unless you use VS Team System you can only use code contracts to check things at runtime, no benefits at compile time.
Meaning that you still have to handle things when something goes wrong.
Not much different from handling a simple exception.Even with VSTS static analysis isn't as a good as the one done at runtime.
This is perfectly understandable, still it's another sign that this feature is meant for runtime usage.
Please correct me if I'm wrong but from what I see there's no way Code Contracts can make my life easier, and my programs more robust, like non-nullable reference types would.
Don't get me wrong, I don't dislike code contracts.
They are a very nice enhancement to the whole framework.
It's just that if this doesn't fill the gap that C# leaves by not having non-nullable reference types, at this point I'm afraid that nothing will.
What do you think?