Link to home
Start Free TrialLog in
Avatar of breezback
breezbackFlag for Israel

asked on

Out Parameter in Recursive Function

Hi,

I have a function that adds elements from tree to a list recursively.
I would like to put the list initialization inside the implementation of the function (There's no good reason, just to understand how)
and pass the list argument as out parameter.

The thing is when I do it I get the error saying I must assign value, because I only initialize the list the first time if it is null the else condition doesn't have initialization.
How should I write it.

Here's the pseudo code:

public func(Tree root, out IList list)
{
.....
if (root == null)
      return;
if (list == null)
      list = new List();
func(root.child,out list);
....
}

Avatar of RishadanPort
RishadanPort

maybe instead use the ref key?

public func(Tree root, ref IList list)
{
.....
if (root == null)
      return;
if (list == null)
      list = new List();
func(root.child,ref list);
....
}
ASKER CERTIFIED SOLUTION
Avatar of RishadanPort
RishadanPort

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I should get credit for this question, as the answer I specified is correct.
Thanks