在属性中传递 ref 类型,C# 是按值传递还是按引用传递

发布于 08-23 01:44 字数 254 浏览 11 评论 0原文

使用 c#、vs2008、winforms

如果我通过属性从父窗体向子窗体传递参数,或者实际上通过属性向任何类传递参数,并且在 C# 中传递的参数是在父窗体中创建的引用类型, 默认情况下它是否作为引用或值传递?

例如,如果我通过属性传递数据集。 如果它确实按值传递,您可以通过属性将其通过 ref 传递吗? 或者我应该通过方法参数传递它,这是更好的做法吗?

基本上我想将填充的对象检索回父表单,并且感觉通过 ref 传递在父表单中创建的对象更好。

using c#, vs2008, winforms

If i am passing a parameter via a property to a child form from a parent form, or infact via a property to any class, and the the paramater im passing in C# is a reference type created in the parent form,
does it get passed as a ref or value by default ?

Eg if i pass a dataset via a property.
And if it does get passed by value, can you make it passed by ref via a property ?
Or should i just pass it via a method paramater, is that better practice ?

Basicaly i want to retrieve a populated object back to the parent form, and feel passing by ref an object that is created in the parent form is better.

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

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

发布评论

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

评论(4

悍妇囚夫2024-08-30 01:44:39

对于引用类型,对变量的引用是按值传递的。当然,除非您在 C# 中使用 refout 关键字来改变该行为。

这意味着 DataSet 值属性实际上传递对 DataSet 实例的引用,并按值传递它。

Jon Skeet 的《C# 深度》 是一个很好的参考(没有双关语)在这些问题上。

For reference types, a reference to the variable is passed by value. Unless, of course, you use the ref or out keywords in C# to alter that behaviour.

That means that a DataSet-valued property passes, in actual fact, a reference to a DataSet instance, and passes it by value.

Jon Skeet's "C# in Depth" is a great reference (no pun intended) on these matters.

剩一世无双2024-08-30 01:44:39

需要注意的是,C# 中的引用传递具有特定的含义。对于属性,该属性最终指向与它所设置的对象相同的地址。在将对象传递给函数的情况下,C# 使用按值传递引用语义。这意味着引用本身被复制,因此新指针指向与传递的对象相同的地址。这可以防止函数通过将其参数设置为 null 来使任何原始指针无效。要实际传递原始引用,必须使用 'ref' 关键字:

class SomeClass
{
  public object MyObjectProperty { get; set; }
}

var someClass = new SomeClass();
object someObject = new object();

someClass.MyObjectProperty = someObject; // Makes MyObjectProperty point to the same location as someObject

在以下情况下,使用按值引用语义:

void MyMethod(object someObject)
{
   someObject = null;
}

object someObject = new object();
MyMethod(someObject);
Console.WriteLine(someObject == null); // Prints false

在以下情况下,使用实际按引用传递语义:

void MyMethod(ref object someObject)
{
   someObject = null;
}

object someObject = new object();
MyMethod(ref someObject);
Console.WriteLine(someObject == null); // Prints true

It is important to note that pass by reference in C# has a specific meaning. In the case of a property, the property ends up pointing to the same address as that of the object it was set to. In the case of passing objects to a function, C# uses pass reference by value semantics. That means that the reference itself is copied, so a new pointer points to the same address as the object that was passed. This prevents a function from nullifying any original pointer by setting its parameters to null. To actually pass an original reference, the 'ref' keyword must be used:

class SomeClass
{
  public object MyObjectProperty { get; set; }
}

var someClass = new SomeClass();
object someObject = new object();

someClass.MyObjectProperty = someObject; // Makes MyObjectProperty point to the same location as someObject

In the following case, reference by value semantics are used:

void MyMethod(object someObject)
{
   someObject = null;
}

object someObject = new object();
MyMethod(someObject);
Console.WriteLine(someObject == null); // Prints false

In the following case, actual pass by reference semantics are used:

void MyMethod(ref object someObject)
{
   someObject = null;
}

object someObject = new object();
MyMethod(ref someObject);
Console.WriteLine(someObject == null); // Prints true
冰雪之触2024-08-30 01:44:39

首先要了解的是,在.Net 中,变量可以是值类型(例如int)或引用类型(类)。值类型变量直接指向内存中的值,而引用类型变量则指向内存位置。

默认情况下,参数按值传递。但是,请记住引用类型的“值”实际上是它在内存中的位置。因此,尽管它被称为按值传递,但实际上是传递对特定类的引用。

The first thing to understand is that, in .Net, a variable can be either a value type (e.g int) or a reference type (a class). Value type variables point directly to a value in memory, whereas reference type variables point to a memory location.

By default, parameters are passed by value. However, remember that the 'value' of a reference type is actually its location in memory. So even though it's called passing by value, you are really passing a reference to a particular class.

貪欢2024-08-30 01:44:39

C# 不像 C++ 那样通过引用传递。更准确的说法是它通过引用值传递(无论如何对于引用类型)。

阅读 Jon Skeet 的这篇文章

C# does not pass by reference in the same way that C++ does. It is more accurate to say that it passes by reference value (for reference types anyways).

Read this by Jon Skeet

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