Design By Contract in C#3.0
-
29-09-2019 - |
Question
I know that C# 4.0 has the Code Contract
feature that can be used to implement post-condition and pre-condition. But I am wanting to implement it using C# 3.0 only. I am experimenting to use this feature in my work. Is it possible to use attributes
to implement post-conditions and pre-conditions?
Any advise?
Thanks.
Solution
You can use Code Contracts with C# 3 and .NET 3.5. The main difference is that the core classes (not the tools) are built into .NET 4 already.
OTHER TIPS
I use a mere Debug.Assert instruction for preconditions and postconditions, and I manually write a bool Invariant()
function, which I call as Debug.Assert(Invariant())
on entry and exit of relevant/nontrivial public methods.
It's manual work, ok, but it's very easy and you get used to it quickly. Also, it allows me to follow Eiffel's design by contract philosophy very closely.