Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using out parameter in a function that has a return value

Tags:

c#

I have a function such as this one:

public string MyFunction(int a, out int b)
{
   var test = ""
   b = 6;
   return test;
}

and then on the receiving end:

int b = 0;
var testOutcome = MyFunction(3, b);

I wonder how to get the value of: b in this scenario?

something like:

var bOutcome = ....; 
like image 974
t_plusplus Avatar asked Jul 31 '26 11:07

t_plusplus


1 Answers

You get the out parameter from the method. Note that you also need to add the out keyword in the parameter signature of the method:

int b = 0;  // initialization is redundant
string  testOutcome = MyFunction(3, out b);
// b is initialized now

Although variables passed as out arguments do not have to be initialized before being passed, the called method is required to assign a value before the method returns.

like image 127
Tim Schmelter Avatar answered Aug 02 '26 01:08

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!