Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c递归方法参数_C#_Recursion_Methods_Syntax_Parameters - Fatal编程技术网

C# c递归方法参数

C# c递归方法参数,c#,recursion,methods,syntax,parameters,C#,Recursion,Methods,Syntax,Parameters,这并不重要。我只是想知道。提前感谢您的帮助 void AddAccount(string name, string surname, DateTime age, string phone, string username, string pwd) { // Codes... // I want to call this method again. // This a example. if(Msg("Registration is available.

这并不重要。我只是想知道。提前感谢您的帮助

void AddAccount(string name, string surname, DateTime age, string phone, string username, string pwd)
{
     // Codes...
     // I want to call this method again.
     // This a example.


     if(Msg("Registration is available. Add it again."))
        AddAccount(name, surname, age, phone, username, pwd);
}
是否有一种方法可以自动获取参数


我知道我可以做得不同。但是我只是想知道这样一种语法的存在。

我认为您正在寻找的是一种避免再次指定参数的快捷方式:

void AddAccount(string name, string surname, DateTime age, string phone, string username, string pwd)
{
     // Codes...
     // I want to call this method again.

     // Is there a method that automatically takes parameters instead?
     AddAccount(params);
}
C中不存在此类语法。另一种方法是创建参数类型:

private struct Person 
{
    public string Name;
    public string Surname;
    public DateTime Age;
    public string Phone;
    public string Username;
    public string Password;
}
然后您可以拥有一个私有重载,该重载接受param类:

void AddAccount(string name, string surname, DateTime age, string phone, string username, string pwd)
{
     Person person = new Person
     {
        Name = name,
        Surname = surname,
        Age = age,
        Phone = phone,
        Username = username,
        Password = password
     }
     AddAccount(person);
}

private void AddAccount(Person person)
{
     // Codes...

     // I want to call this method again.
     AddAccount(person);
}

你应该用文本而不是代码提问。然后为上下文或示例提供一个代码示例。您的问题非常不清楚。你到底在问什么?你已经展示了一个递归的例子,但是它将永远运行良好,直到你得到一个StackOverflowException。通常,如果您想要递归调用,您需要检查一些条件,以便在某个时候停止递归。你想做什么?谢谢你的回复。我只想在使用相同参数的代码中调用此方法。这将通过确认消息框进行处理。感谢您的回复。这是一个较长的解决方案,但重写这些参数。我想学习降低代码复杂性。感谢您了解到没有这样的词。另一个好处是它允许更好的类型安全性。您可以在第二次调用中轻松地切换名称和姓氏参数,编译器也不会抱怨,但您在运行时显然会遇到问题。我将在某些地方使用它。多谢各位@斯坦利酒店