在 C# 中通过引用或不通过引用传递对象
假设我有一个这样的类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于 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();
.由于 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.
将
out
视为使参数充当返回值的一种方式。所以它们非常相似:
然后将
ref
视为允许参数既是输入又是输出的一种方式。因此,在您的示例中,如果您声明了您的方法:
那么调用者必须编写如下内容:
这将是非法的,例如:
他们将被迫传递变量名,而不是表达式,以便
AddToList
方法可以在变量中存储一个值。通过添加 ref 前缀,您可以让您的方法使传递的变量指向不同的对象。Think of
out
as a way of making a parameter work as a return value.So these are very similar:
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:
Then the caller would have to write something like:
This would be illegal, for example:
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 theref
prefix you are allowing your method to make the passed variable point to a different object.如果您需要使用参数 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