I'm a fan of the "fail early" strategy and want to check that methods params have correct values for example. In Java I'd use something like Guava:
checkArgument(count > 0, "must be positive: %s", count);
Is there something similar for .NET?
Requires method. Preconditions specify state when a method is invoked. They are generally used to specify valid parameter values. All members that are mentioned in preconditions must be at least as accessible as the method itself; otherwise, the precondition might not be understood by all callers of a method.
Abstract: Code Contracts API includes classes for static and runtime checks of code and allows you to define preconditions, postconditions, and invariants within a method. The Contracts class is found in the System. Diagnostics namespace.
What you want to do is Design By Contract.
You should use Code Contracts for defining contracts i.e. Preconditions, post-conditions and invariants for your types\methods in C#.
IMO the best and most comprehensive coverage of code-contracts is here.
Code contracts: http://msdn.microsoft.com/en-us/devlabs/dd491992
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With