Can you return multiple values from a function in C#?
Returning multiple values from a function is not possible.Using some other features offered by C#, we can return multiple values to the caller method.This post provides an overview of some of the available alternatives to accomplish this.
We can use the ref keyword to return a value to the caller by reference. We can use it to return multiple values from a method
using System;public class Example{ private static void fun(ref int x, ref int y) { x = 1; y = 2; } public static void Main() { int x = 0; int y = 0; fun(ref x, ref y); Console.WriteLine("x = {0}, y = {1}", x, y); }}/* Output: x = 1, y = 2*/
using System;
public class Example
{
private static void fun(ref int x, ref int y)
x = 1;
y = 2;
}
public static void Main()
int x = 0;
int y = 0;
fun(ref x, ref y);
Console.WriteLine("x = {0}, y = {1}", x, y);
/*
Output: x = 1, y = 2
*/
The out keyword causes arguments to be passed by reference. It is like the ref keyword, except that ref requires that the variable be initialized before it is passed
using System;public class Example{ private static void fun(out int x, out int y) { x = 1; y = 2; } public static void Main() { int x = 0; int y = 0; fun(out x, out y); Console.WriteLine("x = {0}, y = {1}", x, y); }}/* Output: x = 1, y = 2*/
private static void fun(out int x, out int y)
fun(out x, out y);
Yes, its possible
As you use as tuple type.
private static (int, string) func(int input){ return (input, "sample string");}public static void Main() { var (index, text) = func(1); Console.WriteLine($"index= {index}, text= {text}");}
private static (int, string) func(int input){
return (input, "sample string");
public static void Main() {
var (index, text) = func(1);
Console.WriteLine($"index= {index}, text= {text}");
Yes we can pass using out parameters in the method
Yes