I wonder if there is a less verbose way to do Input Verification in my methods. For example, i commonly write stuff like this:
public string SomeFunction(string param1, int param2) { if(string.IsNullOrEmpty(param1)){ throw new ArgumentException('bla', 'param1'); } if(param2 < 0 || param2 > 100 || param2 == 53) { throw new ArgumentOutOfRangeException('eek', 'param2'); } }
Now, I wonder if there is a way to set up constraints on the parameters and have the compiler already handle that for me? I believe that this is called ‘Contract’ and I remember seeing that Spec# is supposed to do that, but that seems to be an experimental research project at the moment.
So I wonder: Is there anything that can give a clean enforcing of Constraints (at least the simple and often recurring ones like string.IsNullOrEmpty) for input parameters for .net 3.5 SP1 and ideally .net 3.0 already?
Three words: design by contract
One implementation for C# can be found here: http://www.codeproject.com/KB/cs/designbycontract.aspx