从方法中返回三个值

flo*_*oyd 0 c# return class

我有这个解决方案,我正在努力,并尝试将3个不同问题的userInput返回给Main.这就是我目前所拥有的.

public static string GetInput()
{
    //declared variables
    string userInput;
    string outputTitle;

    Console.Write("Enter your name: ");
    userInput= Console.ReadLine();

    Console.Write("Enter your age: ");
    userInput = Console.ReadLine();

    Console.Write("Enter the Gas Mileage: ");
    userInput = Console.ReadLine();

    return userInput;


}
Run Code Online (Sandbox Code Playgroud)

我设置它的方式我永远不会通过("输入你的名字"); 题.有什么更好的方法来解决这个问题?

userName = InputUtilities.GetInput(userName); //错误2方法'GetInput'没有重载需要1个参数

Console.Write("你的名字是:{0}",userName);

Bre*_*een 9

使用类来定义要捕获的输入,并返回该输入.

    public class UserDetails
    {
        public string Name {get;set;}
        public string Age {get;set;}
        public string Mileage {get;set;}
    }

    public static UserDetails GetInput()
    {
        //declared variables
        //string userInput;
        //string outputTitle;
        var userD = new UserDetails();

        Console.Write("Enter your name: ");
        userD.Name = Console.ReadLine();

        Console.Write("Enter your age: ");
        userD.Age = Console.ReadLine();

        Console.Write("Enter the Gas Mileage: ");
        userD.Mileage = Console.ReadLine();

        return userD;
    }
Run Code Online (Sandbox Code Playgroud)

编辑:使用输出参数的示例.

void Main()
{
    string name, age, mileage;

    GetInput(out name, out age, out mileage);

    //use name, age and mileage here.
}

public static void GetInput(out string pName, out string pAge, out string pMileage)
{
    Console.Write("Enter your name: ");
    pName = Console.ReadLine();

    Console.Write("Enter your age: ");
    pAge = Console.ReadLine();

    Console.Write("Enter the Gas Mileage: ");
    pMileage = Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)