views:

88

answers:

7

This is mostly just out of curiosity, and is potentially a silly question. :)

I have a method like this:

public void MyMethod(string arg1, string arg2, int arg3, string arg4, MyClass arg5)
{
    // some magic here
}

None of the arguments can be null, and none of the string arguments can equal String.Empty.

Instead of me having a big list of:

if(arg1 == string.Empty || arg1 == null)
{
    throw new ArgumentException("issue with arg1");
}

is there a quicker way to just check all the string arguments?

Apologies if my question isn't clear.

Thanks!

+3  A: 
if (string.IsNullOrEmpty(arg1))
{
    throw new ArgumentException("issue with arg1");
}

Also there are code contracts in .NET 4.

Darin Dimitrov
+7  A: 

You can create or use frameworks to check contract of your method, e.g. Code Contracts.

Also you can create various utility methods like ThrowIfNullOrEmpty who will encapsulate logic of checking arguments.

Andrew Bezzub
Thanks! Looking into these!
AndyC
+4  A: 

You can use String.IsNullOrEmpty:

if (String.IsNullOrEmpty(arg1) )
    throw new ArgumentException("issue with arg1");

If you're on Framework 4, there is also a String.IsNullOrWhiteSpace method (perhaps you also want to throw if someone calls the method with a string containing of spaces only).

You can make a utility method to check an argument and throw, but that is the shortest notation you will get. There is no syntax in C# that allows you to express to the compiler, that nulls are unacceptable, and have the language throw the exceptions for you.

With framework 4, you also have Code Contracts - but the syntax to use it, remains a method call.

driis
This is cool, though I'm wondering if there was an even faster way. Thinking out loud here, but it would be great if there was some form of shorthand notation that could be appended to arguments in a methods signature so that C# will automatically throw an argument exception if it was null or empty.
AndyC
A: 

Well, first of all, you can use if (!String.IsNullOrEmpty(arg1)) to reduce the if expression.

Second, do you need argument checking in your release build, or would it suffice to perform arg checking in your debug build? It depends on where the data is coming from. If this function is being called by your own code and the args are processed by your calling code, it would probably suffice to check the args in your debug builds only. In that case, use Debug.Assert(!String.IsNullOrEmpty(arg1), "msg"); etc. Asserts are stripped out of release builds.

Third, check out DevLabs: Code Contracts

dthorpe
+1  A: 

The real problem here is that your method has too many arguments. That makes it unwieldy for you to write the validation code. Makes it unwieldy on the client code as well. Consider writing a helper class where the arguments you now pass are properties. Your method could now take only one argument. And the validation can be done in the class itself instead of your method.

Hans Passant
+1  A: 

There's a nice library called CuttingEdge.Conditions that provides a fluent interface for this:

public void MyMethod(string arg1, string arg2, int arg3, string arg4, MyClass arg5)
{
    Condition.Requires(arg1, "arg1").IsNotNull().IsNotEmpty().StartsWith("Foo");
}

It provides type safe conditions for all sorts of validations, including null checks, range checking on numbers, string checking (including the StartsWith above), etc... If you have multiple checks for parameters, it can dramatically shorten and simplify your validation, and most importantly, make the argument checking and exceptions consistent throughout your application.

Reed Copsey
A: 

As far as I could understand your question, how about that:

static void Main(string[] args)
{
    Test(null, "", "", "");
}

static void Test(MyClass arg1, params string[] args)
{
    if (args.Count(a => String.IsNullOrEmpty(a)) == args.Length)
    {
        throw new Exception("All params are null or empty");
    }
}
abatishchev
ANeves
@sr pt: Thanks! Excellent tip about `Any()`
abatishchev