我有大约8-9个参数要传递给一个返回数组的函数。我想知道直接在函数中传递这些参数还是传递数组更好?哪种方式更好?为什么?
发布于 2008-12-02 04:35:30
如果我要做什么的话,那就是创建一个包含所有参数的结构,以获得良好的智能性和强名称。
public struct user
{
public string FirstName;
public string LastName;
public string zilionotherproperties;
public bool SearchByLastNameOnly;
}
public user[] GetUserData(user usr)
{
//search for users using passed data and return an array of users.
} 发布于 2008-12-02 04:34:46
逐个传递它们,因为:
作为类型安全way.
但是,如果参数确实是数组,则传递数组。示例:
对于如下所示的函数,请使用以下表示法:
Array FireEmployee(string first, string middle, string last, int id) {...}对于如下所示的函数,请使用数组:
Array FireEmployees(Employee[] unionWorkers) {...}发布于 2008-12-02 07:04:38
Martin Fowler的refactoring一书中的Introduce Object refactoring介绍了您的场景。这本书非常值得拥有,但对于那些不值得拥有的人来说,重构被描述为here。publisher's site和谷歌图书上也有预览版。它建议不要将参数替换为数组,而是替换为新对象。
https://stackoverflow.com/questions/333056
复制相似问题