我一直在使用Visual Studio中的C#编写软件。但我想在我的方法中返回多个值,如何在C#中返回多个值?是否有可能?
我一直在使用Visual Studio中的C#编写软件。但我想在我的方法中返回多个值,如何在C#中返回多个值?是否有可能?
public Tuple<int, int> GetMultipleValue()
{
return Tuple.Create(1,2);
}
现在更容易了。
例如:
public (int, int) GetMultipleValue()
{
return (1,2);
}
你想要返回单一类型的值吗?那么你可以返回一个值数组。如果你想要返回不同类型的值,你可以创建一个带有指定参数的对象、结构体或元组,然后将这些值赋给这些参数。创建对象后,你可以从方法中返回它。
使用对象的示例:
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;
}
}
使用结构体的示例:
public struct DataContainer
{
public stringstringType;
public int intType;
public bool boolType;
}
一个元组的例子:
var DataContainer= new Tuple<string, int, bool>(stringValue, intValue, boolValue);