C# 返回函数的值而不将其赋值给变量

C# 返回函数的值而不将其赋值给变量,c#,C#,所以我要做的是获取一个用户输入列表,并将它们直接放入一个数组中。我不想为每个输入分配一个变量,因为可能有数百个 static void writeAndWait(String statement, int millisecondsToWait) { Console.WriteLine(statement); Thread.Sleep(millisecondsToWait); return; } static

所以我要做的是获取一个用户输入列表,并将它们直接放入一个数组中。我不想为每个输入分配一个变量,因为可能有数百个

        static void writeAndWait(String statement, int millisecondsToWait)
    {
        Console.WriteLine(statement);
        Thread.Sleep(millisecondsToWait);
        return;
    }
    static void Main(string[] args)
    {
        //I am using ArrayLists because they will store as many values as needed wether it be 1 or 1,000,000 or more
        ArrayList Name; //Declaring Name  
        ArrayList Time; //Declaring Time
        ArrayList Path; //Declaring Path
        Name = new ArrayList(); //Name will be used to store the names of the timers the user inputs
        Time = new ArrayList(); //Time will be used to store the times of the timers the user inputs
        Path = new ArrayList(); //Path will be used to store the path line of the timers the user inuts;

        writeAndWait("Hello, if you want to add timers all you need to do is type a name and press enter, say how long you want the timer to run for in minutes, and then add a number 1-10 any timers with the same number at the end will run sycrnasly, and any timers with diferant nubers will run async", 2000);
        Name.Add(Console.ReadKey().ToString());
        Console.WriteLine(Name[0]);
    }
Console.WriteLine只返回“Console.ReadKey().ToString())


我希望它返回用户输入的密钥。或者Console.ReadKey的返回值不确定您在这里要问什么,但是使用struct可以避免您需要维护单独的数组。如果你有一个动态数量的参数,我可以修改这个答案

public struct TimerDescriptor
{
    public string Name;
    public string Time;
    public string Path;

    public static bool TryParse(string text, out TimerDescriptor value)
    {
        //check for empty text
        if (string.IsNullOrWhiteSpace(text))
        {
            value = default(TimerDescriptor);
            return false;
        }

        //check for wrong number of arguments
        var split = text.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries);
        if (split.Length != 3)
        {
            value = default(TimerDescriptor);
            return false;
        }

        value = new TimerDescriptor
        {
            Name = split[0],
            Time = split[1],
            Path = split[2]
        };

        return true;
    }

    public override string ToString()
    {
        return Name + ' ' + Time + ' ' + Path;
    }
}

static void writeAndWait(String statement, int millisecondsToWait)
{
    Console.WriteLine(statement);
    Thread.Sleep(millisecondsToWait);
}
static void Main(string[] args)
{
    //I am using ArrayLists because they will store as many values as needed wether it be 1 or 1,000,000 or more
    var timerDescriptors = new List<TimerDescriptor>();

    writeAndWait("Hello, if you want to add timers all you need to do is type a name and press enter, say how long you want the timer to run for in minutes, and then add a number 1-10 any timers with the same number at the end will run sycrnasly, and any timers with diferant nubers will run async", 2000);

    var line = Console.ReadLine();
    TimerDescriptor descriptor;
    if (TimerDescriptor.TryParse(line, out descriptor))
    {
        timerDescriptors.Add(descriptor);
        Console.WriteLine(descriptor);
    }
    else Console.WriteLine("Syntax error: wrong number of arguments.  The syntax is: {Name} {Time} {Path} without the curly braces.");
}
public struct TimerDescriptor
{
公共字符串名称;
公共字符串时间;
公共字符串路径;
公共静态bool TryParse(字符串文本、超时脚本值)
{
//检查是否有空文本
if(string.IsNullOrWhiteSpace(text))
{
值=默认值(TimerDescriptor);
返回false;
}
//检查参数的数目是否错误
var split=text.split(新[]{''},StringSplitOptions.RemoveEmptyEntries);
如果(拆分长度!=3)
{
值=默认值(TimerDescriptor);
返回false;
}
值=新的TimerDescriptor
{
名称=拆分[0],
时间=拆分[1],
路径=拆分[2]
};
返回true;
}
公共重写字符串ToString()
{
返回名称+''+时间+''+路径;
}
}
静态void writeAndWait(字符串语句,int毫秒)
{
控制台写入线(语句);
线程睡眠(毫秒级);
}
静态void Main(字符串[]参数)
{
//我之所以使用ArrayList,是因为无论是1还是1000000或更多,它们都将根据需要存储尽可能多的值
var timerDescriptors=新列表();
writeAndWait(“您好,如果您想添加计时器,只需键入一个名称并按enter键,说出您希望计时器以分钟为单位运行多长时间,然后添加数字1-10结尾处具有相同数字的任何计时器都将同步运行,具有不同数字的任何计时器都将异步运行”,2000年);
var line=Console.ReadLine();
计时器描述符;
if(TimerDescriptor.TryParse(行,输出描述符))
{
添加(描述符);
控制台写入线(描述符);
}
else Console.WriteLine(“语法错误:参数数量错误。语法为:{Name}{Time}{Path}不带花括号。”);
}

FYI,您可能想使用而不是
控制台。ReadKey
为什么不使用
List
而不是
ArrayList
?还有,既然您的消息要求键入名称并按enter键,为什么您要使用
Console.ReadKey
而不是
Console.ReadLine
。我在从列表中读取值时遇到了一些问题,这就是我切换到ArrayList的原因。如果你能在演示如何使用列表时对一些代码进行注释,那就太好了。但是仍然存在这样一个问题:如何只返回值而不返回acctuall命令?bool quit=false;变量名称=新列表();Console.WriteLine(“请输入名称”);while(!quit){string input=Console.ReadLine();if(input==string.Empty)quit=true;else names.Add(input);}/*您可以添加它来显示名称*/Console.WriteLine(“您输入了这个名称:”);foreach(名称中的字符串名称){Console.WriteLine(名称);}