Drahcir
Drahcir

Reputation: 11972

method return types

I'm new to C# and I'm trying to figure out the best way to test if a method returns correctly, ie; did the method get the information I wanted.

It's the types that are making this difficult. Is there a way that I can sometimes return bool and sometimes return something else?

In PHP I would create my function like this

function myFunc(){
    //Do Stuff
    if(/*whatever*/) return (string) "abcdefg";
    return false; //otherwise it will just return false
}

Then call it like this to test if it worked or not

if(!$var=myFunc()) die("It Didn't Work");
echo $var;

But with C#, my function has to return a specified type, not a string or bool false if it didn't work out.

If in C# I have a method that returns IntPtr I could have it return (IntPtr) 0 instead of false, but this is messy and there's probably a better way of doing it.

Any Ideas? What's the standard and usual way of doing this in C#?

Upvotes: 4

Views: 323

Answers (6)

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4907

If your Intention is how to return types you can use Tuple<>

public Tuple<string,bool> myFunc()
{
    //Do Stuff
    if(/*whatever*/) return (string) new Tuple<string,bool>("abcdefg",true);
    return new Tuple<string,bool>(null,false);
}

Upvotes: 1

Kiley Naro
Kiley Naro

Reputation: 1769

In your example I would return null from your function in C#:

public string myFunc(){
    //Do Stuff
    if(/*whatever*/) return "abcdefg";
    return null;
}

And then in your calling code:

string myVar = myFunc();

if(null != myVar)
{
    throw new Exception();
}

Console.WriteLine(myVar);

It should be noted though that any time you throw exceptions for non-exceptional circumstances (meaning, if your call to myFunc could, in reasonable usage, return null) you will take a serious performance hit. It takes a long time to generate an exception, so it's generally better to avoid them, perhaps by using a pre-requisite check before even calling myFunc.

In circumstances where you're dealing with non-nullable value types, C# has a concept called Nullable Types.

Upvotes: 1

Kakashi
Kakashi

Reputation: 2195

Personally, I prefer use null.

public string foo()
{
    if (whatever)
    {
        return "abc"; 
    }

    return null; 
}

and Then:

 if(foo() != null) 
  {
    //do something  
  }
  else 
  {
   // *It Didn't Work*
  }

Upvotes: 0

Beku
Beku

Reputation: 405

You can always return null.

string Foo()
{
    if (!success) return null;
    return "abcd";
}

You then check the call like that:

var tmp = Foo();
if (tmp == null) return -1;
// tmp is a string so do your work

The null return will only work with object, unfortunately you can't do that with an int. In that case throw an exception or use a nullable int:

int? Bar()

Upvotes: 4

McKay
McKay

Reputation: 12604

Use Exception based programming:

public string Blah()
{
    if (whatever)
    {
        return "abcdefg";
    }
    throw new Exception("Something went wrong");
}

Or you could return null or string.Empty

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564323

There are typically two approaches here:

  1. Use Exceptions
  2. Use the TryXXX approach.

The appropriate method depends on what this is doing. If the "false" case is really something that's exceptional and not the norm, raising an exception is an appropriate way of handling it.

If, however, you're doing something like parsing user input, where its common that you'll "fail", using the TryXXX approach is a good example. This would look like:

bool TryMyFunc(out string result)
{
     if (...)
     {
         result = "abcdefg";
         return true;
     }

     result = string.Empty;
     return false;
}

This is how Int32.TryParse works in the framework, for example.

Upvotes: 6

Related Questions