c#递归方法参数

时间:2017-08-29 21:08:48

标签: 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. Add it again."))
        AddAccount(name, surname, age, phone, username, pwd);
}

是否有一种自动取参数的方法?

我知道我可以采用不同的方式。但我只是想知道这种语法是否存在。

1 个答案:

答案 0 :(得分:1)

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

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);
}