5

假设一个名为“Place”的类存储位置和相关细节:

class Place
{
    string Name;
    string Location;
    // etc...
}

稍后,我们会填充多达 80 个不同城镇的名称和详细信息。所以我们将这些城镇以数字形式存储在一个数组中:

class Setup
{
    public static GetTowns
    {
        for (int i = 1; i <= numberOfTowns; i++)
        {
            Town[i] = new Place(name, location);
        }
        // more code...
    }
    // more methods...
}

要访问特定城镇及其数据,我们将城镇本身作为参数传递并接收它:

public static void DescribeTown(Place Town)
{
    // example method from some other class
    Console.WriteLine("Shipping to {0}.", Town.Name);
}

其他方法需要访问多个城镇,或所有城镇。然后我们可以传入整个 Town 数组作为参数:

public static void ListAllTowns(Place[] Town)
{
    Console.WriteLine("Our Beloved Clients:");
    foreach (Place here in Town)
    {
        Console.WriteLine("{0} in {1}", here.Name, here.Location);
        // Various other operations requiring info about the Towns
    }
}

完整参考 C# 2.0声明如下

参数是在调用方法时接收传递给方法的参数值的变量。

ListAllTowns这似乎是说类的每个实例以及所涉及的所有数据在每次调用时都会被传入。这对我来说听起来很浪费,但是是吗?

(出于概念验证的原因,此示例被大大简化,但方法需要读/写权限以及多个城镇之间的比较。)

4

6 回答 6

7

There's nothing wasteful about it. You're not passing a copy of all of the items, you're simply creating an array and then passing a reference to that array to the ListAllTowns method (arrays are reference types).

You're doing things (one of) the right way(s).

于 2012-08-21T16:11:21.563 回答
4

Parameters are always passed by value (unless the ref or out keywords are used), but for a reference type the value is not the object itself, but the reference to the object.

So, the entire object is not passed as a parameter, only a reference to the object.

于 2012-08-21T16:12:07.390 回答
2

All classes in C# are references by default, so only a pointer is being send to function.

Answering to your question: it depends. Sometimes it actually does make sense to send a whole object as a parameter (i.e. we are trying to something with an object). On the other hand, creating classes only for function parameters is probably not nice. In your particular example it does make sense to create a class.

One exception from what I've just told: structs are being sent as a copy (all members will be copied). You must be careful while sending these as function parameters.

于 2012-08-21T16:13:17.967 回答
1

这总是一个两难的境地。

一方面 - 奥卡姆剃刀说你不需要任何过度的东西,另一方面 - 更改DescribeTown(Place Town)DescribeTown(string TownName)意味着不以 OOP 方式进行操作,而这反过来又意味着代码将来更难更改和维护。

但有时当系统成熟稳定时,当发现整体Town仍然过度时,您可以将其重构回DescribeTown(string TownName).

另一个方面 - 单元测试。比更容易测试DescribeTown(string TownName)方法DescribeTown(Place Town),后者需要模拟一个 Town 对象,并且你应该模拟哪些属性并不明显(你必须在测试之前知道它)。

于 2012-08-21T16:37:54.587 回答
1

在 .NET 中,对象作为引用保存。在这种情况下,您不是在传递或复制整个数据,而是对该数据的已创建实例的引用。

在这种情况下,鼓励传递整个实例,因为它允许您保持一致性(处理 Towns 的方法将 Towns 作为参数是有道理的)

于 2012-08-21T16:14:59.810 回答
0

我的观点是,将一个类作为参数传递是完全可以的。这得到了包括 Robert Martins Clean Code 在内的多位作者的支持。我还认为,如果可能的话,您应该完全避免传递参数。如果不是将 Town 对象数组传递给 ListAllTowns 方法,而是将“ListAll”设置为 Place 类的方法,那么您会像这样调用该方法:

Towns.ListAll()

从这个意义上说,您不必在每次使用时都将列表传递给函数,因为它只是在其拥有的对象上进行操作。

于 2012-08-21T16:18:13.057 回答