+ 3

Can we return multiple values from a function in c#?

related to parameter passing

7th Dec 2016, 7:24 PM
SHUVHAM ANAND
SHUVHAM ANAND - avatar
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.
7th Dec 2016, 7:27 PM
sumit kumar
sumit kumar - avatar
+ 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.
31st Dec 2016, 8:52 AM
Rishav Prabhraj
Rishav Prabhraj - avatar
+ 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); }
7th Dec 2016, 7:36 PM
Alberto c
Alberto c - avatar
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; }
9th Dec 2016, 9:46 PM
Elie Boustany
Elie Boustany - avatar