C # function returns a method of multiple values

Sometimes we need a function to return multiple values. On the Internet, it is mostly implemented with out. I personally like to use the tuple method.

Tuple is a tuple, which supports up to 7 elements, and more methods such as nesting are needed.

The method of using tuples to define functions is as follows:

public static Tuple<string,string span>> TupleFun()

{
string[] T = {'hello','world'};
Tuple
<string, string> tup = new Tuple<string, string span>>(T[0], T[2]);
return tup;
}

Tuples also support multiple types of values.

public static Tuple <string,int> TupleFun ()

{
string T = ‘hello’;
int q = 6;
Tuple
<string, int> tup = new Tuple<string, int span>>(T, q);
return tup;
}

When calling the function, use Item* to call the elements in the tuple.

var tuple = TupleFun();

print(tuple.Item1);
print(
int.Parse(tuple.Item2));

public static Tuple<string,string> TupleFun()

{
string[] T = {'hello','world'};
Tuple
<string, string> tup = new Tuple<string, string span>>(T[0], T[2]);
return tup;
}

public static Tuple<string,int> TupleFun()

{
string T = ‘hello’;
int q = 6;
Tuple
<string, int> tup = new Tuple<string, int span>>(T, q);
return tup;
}

var tuple = TupleFun();

print(tuple.Item1);
print(
int.Parse(tuple.Item2));

Leave a Comment

Your email address will not be published.