I have been working with my software using C# in visual studio. But i want to return multiple values in my method, How to return multiple values in a method in C#.... Is it possible??
You can use .NET 4.0+'s Tuple:
For example:
public Tuple<int, int> GetMultipleValue()
{
return Tuple.Create(1,2);
}
it's much easier now
For example:
public (int, int) GetMultipleValue()
{
return (1,2);
}
Do you want to return values of a single type? In that case you can return an array of values. If you want to return different type of values you can create an object, struct or a tuple with specified parameters and then assign those values to these parameters. After you create an object you can return it from a method.
Example with an object:
public class DataContainer
{
public string stringType { get; set; }
public int intType { get; set; }
public bool boolType {get; set}
public DataContainer(string string_type, int int__type, bool bool_type)
{
stringType = string_type;
intType = int__type;
boolType = bool_type;
}
}
Example with a struct:
public struct DataContainer
{
public stringstringType;
public int intType;
public bool boolType;
}
Example with a tuple:
var DataContainer= new Tuple<string, int, bool>(stringValue, intValue, boolValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With