Thaadikkaaran
Thaadikkaaran

Reputation: 5226

How to exit from a C# recursive method which has return type

My code is like the below one,

public object GetObjectValue(object obj, int condition)
{
     if(condition > 10)
     {
       //exit from method 
       // return; gives compiler error.      
     }
     else
     {
        GetObjectValue(obj,condition); // like this i need to use recursive call.
        //Do stuffs
     }
}

How to exit from this method. Help me.

Upvotes: 7

Views: 36432

Answers (4)

Nanda
Nanda

Reputation: 614

You have specified the return type as object, so have to specify the valid return type. Try the below code change,

public object GetObjectValue(object obj, int condition)
{
    if (condition <= 10)
    {
        GetObjectValue(obj, condition);
    }
    return null;   //return whatever object you have to return
}

Upvotes: 1

Kobi
Kobi

Reputation: 138007

Some points:

  1. You need to return something in either case. Even if you return null on if(condition > 10), your next compilation error will say you need to return a value on every path (actually, that's the same compilation error)
  2. GetObjectValue(obj,condition); may result in an infinite recursion - you call it with the same values over and over.
  3. You cannot "do stuff" after a return statement - that marks the end of the executed code (unless you have a finally block, but that's another subject). If you don't want to return that value that's also great, but you probably want to use it somehow: object returnedValue = GetObjectValue(obj, condition);

You may be looking for something like:

public object GetObjectValue(object obj, int condition)
{
     if(condition > 10)
     {
       //exit from method 
       return null; 
     }
     else
     {
        IChild current = (IChild)obj
        //Do stuffs HERE
        return GetObjectValue(current.Parent, condition + 1);  recursive call.
     }
}

Upvotes: 11

Quick Joe Smith
Quick Joe Smith

Reputation: 8222

You need to return an object reference, or null.

public object GetObjectValue(object obj, int condition) {
    if (condition > 10) {
        return null;
    } else {
        return GetObjectValue(obj, condition);
    }
}

Be aware though, that this method is very prone to a stack overflow error because any value of condition less than or equal to 10 will never reach the base case, or terminating condition. Recursive methods need a case that returns a value without calling itself.

public object GetObjectValue(object obj, int condition) {
    if (condition > 10) {
        return null;
    } else {
        return GetObjectValue(obj, condition++);
    }
}

Now condition is incremented, so when the method is called, it will eventually progress towards a value greater than 10, satisfying the condition and returning null. This is still a useless method though, because it can only ever return the same value for any input; the only difference will be how many recursive calls it makes before returning null.

Upvotes: 4

Oded
Oded

Reputation: 498934

You are supposed to return an object:

public object GetObjectValue(object obj, int condition)

Either change the return type:

public void GetObjectValue(object obj, int condition)

Or use a valid return statement:

if(condition > 10)
 {
   //exit from method 
   return null;
 }

Upvotes: 1

Related Questions