在 C# 中通过引用或不通过引用传递对象

发布于 2024-09-01 11:02:38 字数 602 浏览 4 评论 0原文

假设我有一个这样的类:

public class ThingManager {
    List<SomeClass> ItemList;

    public void AddToList (SomeClass Item)
    {
        ItemList.Add(Item);
    }

    public void ProcessListItems()
    {
        // go through list one item at a time, get item from list,
        // modify item according to class' purpose
    }
}

假设“SomeClass”是一个相当大的类,包含非常复杂的方法和成员(例如 List<> 和数组),并且它们的数量可能很大,所以不是复制程序中的大量数据非常重要。

“AddToList”方法中是否应该包含“ref”?为什么?

这就像尝试再次学习 C 中的指针;-)(这可能就是我感到困惑的原因,我试图将这些与指针联系起来。在 C 中,它是“SomeClass *Item”和一个“列表” SomeClass *" 变量)

Suppose I have a class like this:

public class ThingManager {
    List<SomeClass> ItemList;

    public void AddToList (SomeClass Item)
    {
        ItemList.Add(Item);
    }

    public void ProcessListItems()
    {
        // go through list one item at a time, get item from list,
        // modify item according to class' purpose
    }
}

Assume "SomeClass" is a fairly large class containing methods and members that are quite complex (List<>s and arrays, for example) and that there may be a large quantity of them, so not copying vast amounts of data around the program is important.

Should the "AddToList" method have "ref" in it or not? And why?

It's like trying to learn pointers in C all over again ;-) (which is probably why I am getting confused, I'm trying to relate these to pointers. In C it'd be "SomeClass *Item" and a list of "SomeClass *" variables)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

汐鸠 2024-09-08 11:02:38

由于 SomeClass 是一个类,因此它会通过引用自动传递到 AddToList 方法(或更准确地说,它的引用通过值传递),因此不会复制对象。如果您想在 AddToList 方法中重新分配引用指向的对象,则只需使用 ref 关键字,例如 Item = new SomeClass();

Since SomeClass is a class, then it is automatically passed by reference to the AddToList method (or more accurately, its reference is passed by value) so the object is not copied. You only need to use the ref keyword if you want to re-assign the object the reference points to in the AddToList method e.g. Item = new SomeClass();.

初见你 2024-09-08 11:02:38

由于 SomeClass 是引用类型,因此不需要使用“ref”关键字。如果它是值类型,“ref”可能有用。

Since SomeClass is a reference type, you do not need to use the "ref" keyword. If it were a value type, "ref" might be useful.

孤蝉 2024-09-08 11:02:38

out 视为使参数充当返回值的一种方式。

所以它们非常相似:

void Foo(out int result)
{
    result = 5;
}

int Foo()
{
    return 5;
}

然后将 ref 视为允许参数既是输入又是输出的一种方式。

因此,在您的示例中,如果您声明了您的方法:

public void AddToList(ref SomeClass Item)

那么调用者必须编写如下内容:

SomeClass i = null;
obj.AddToList(ref i);

这将是非法的,例如:

obj.AddToList(ref new SomeClass());

他们将被迫传递变量名,而不是表达式,以便 AddToList方法可以在变量中存储一个值。通过添加 ref 前缀,您可以让您的方法使传递的变量指向不同的对象。

Think of out as a way of making a parameter work as a return value.

So these are very similar:

void Foo(out int result)
{
    result = 5;
}

int Foo()
{
    return 5;
}

And then think of ref as a way of allowing a parameter to be both an input and an output.

So in your example, if you declared your method:

public void AddToList(ref SomeClass Item)

Then the caller would have to write something like:

SomeClass i = null;
obj.AddToList(ref i);

This would be illegal, for example:

obj.AddToList(ref new SomeClass());

They would be forced to pass a variable name, rather than an expression, so that the AddToList method can store a value in the variable. By adding the ref prefix you are allowing your method to make the passed variable point to a different object.

沙沙粒小 2024-09-08 11:02:38

如果您需要使用参数 user ref 的原始值。如果没有,请用完。供参考:

http://www.yoda.arachsys.com/csharp/parameters.html

http://msdn.microsoft.com /en-us/library/0f66670z(VS.71).aspx

If you ever need to use the original value of the parameter user ref. If not, use out. For reference:

http://www.yoda.arachsys.com/csharp/parameters.html

http://msdn.microsoft.com/en-us/library/0f66670z(VS.71).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文