+ 3
Can we return multiple values from a function in c#?
related to parameter passing
4 Answers
+ 2
yes...using output parameters.A return statement can be used for returning only one value from a function.however,using output parameters you can return two values from a function.
+ 2
Output parameters are similar to reference parameters, except that they transfer data out of the method rather than accept data in. They are defined using the out keyword.
The variable supplied for the output parameter need not be initialized since that value will not be used. Output parameters are particularly useful when you need to return multiple values from a method.
+ 1
yes, you can return values from a function.
this is the formula to create a function:
access modifier+(optional can be static or not) + return type value + identifier + parameters
Example:
public static int calculate(int a, int b)
{
return a+b; // this function will return the value of the sum (a+b);
}
0
You can use an object to return as many values as you want for example
public class Employee
{
public int x;
public int y;
}
//this function returns an Employee object
public Employee ReturnEmployee()
{
Employee emp1 = new Employee();
emp1.x = 2;
emp1.y = 3;
return emp1;
}