2017-08-29 102 views
-3

这并不重要。我只是想知道。预先感谢您的帮助。c#递归方法参数

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

是否有一种方法可以自动取代参数?

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

+0

你应该问你的问题在文本中,而不是在代码。然后提供上下文或示例的代码示例。 –

+0

你的问题很不清楚。你到底在问什么? – Igor

+0

你已经展示了一个递归的例子,但它会永远运行(好吧,直到你得到一个'StackOverflowException')。通常情况下,如果你想递归调用,你会有一些条件,你会检查它,在某些时候,会停止递归。你想做什么? –

回答

1

我认为你在寻找什么是快捷方式,以避免再次指定参数:在C#中存在

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

没有这样的语法。另一种方法是创建一个参数类型:

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

感谢您的回复。这是一个更长的时间,但重写这些参数的解决方案。我想学会减少代码的复杂性。谢谢你了解到没有这个词。 – Emre

+0

另一个好处是它可以提供更好的类型安全性。你可以在第二次调用时轻松切换'name'和'surname'参数,编译器不会抱怨,但是在运行时显然会遇到问题。 –

+0

我会在一些地方使用它。谢谢。 @D史丹利 – Emre