I know two ways to check parameters of the method and throw exceptions when it is needed.
1) Check one each parameter and throw an exception when it is wrong:
public void Method(object parameter1, object parameter2)
{
if (parameter1 == null)
{
throw new ArgumentNullException("parameter1");
}
if (parameter2 == null)
{
throw new ArgumentNullException("parameter2");
}
...
}
2) Check all parameters at once and throw same exception for all:
public void Method(object parameter1, object parameter2)
{
if (parameter1 == null || parameter2 == null)
{
throw new ArgumentNullException();
}
...
}
The first approach is, in my opinion, better and cleaner, but also cover a lot of lines. For example, a method that actually do the 2 lines of code - in this way code will increase by 4 rows (including blank line) for each parameter.
I am interested in is the approach used by experienced programmers. Are there better ways than these two?
Parameters are variables defined in the method declaration after the method name, inside the parentheses. This includes primitive types such as int, float, boolean, etc, and non-primitive or object types such as an array, String, etc. You can pass values(Argument) to the method parameters, at the method call.
They are few keywords that are use to control how arguments are passed to the method. (None): If parameter is not marked with a parameter modifier,it is assumed to be passed by value, meaning the called method receives a copy of the original data. static int Add(int x,int y){
It provides an ease for user because sometimes we may have 10-20 parameters in a method.
1) Check one each parameter and throw an exception when it is wrong: public void Method(object parameter1, object parameter2) { if (parameter1 == null) { throw new ArgumentNullException("parameter1"); } if (parameter2 == null) { throw new ArgumentNullException("parameter2"); } ... }
Updated July 2020
Check out this blog post on how you can achieve a similar approach to Code Contracts.
https://enterprisecraftsmanship.com/posts/code-contracts-vs-input-validation/
Original answer provided below
—-
If you are using .NET framework 4, check out Code Contracts, which simplifies it down to a single line of code
public string Reverse(string text)
{
Contract.Requires<ArgumentNullException>(text!=null, "ParAmeter cannot be null.");
.....
}
The reason you would use this is because you can now get automated tools like Pex to tell you what unit tests to apply to this method. It also gives you feedback at compile time if this method would throw an exception based on how you are calling it. Like
String text = null;
String reversedString = Reverse(text);
The compiler will warn you that this will throw an exception.
Note Code Contracts needs an add-in to be installed, but it is free.
Use method attribute to cleanly check your parameters. i was wrote a framework for parameter validating in python.the c# best practice is here
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