如何在 C# 中传递 const 引用?

发布于 2024-08-03 12:50:09 字数 711 浏览 4 评论 0原文

在 C++ 中,传递 const 引用是一种常见的做法 - 例如:

#include <iostream>
using namespace std;

class X
{
  public :
    X()                              {m_x = 0; }
    X(const int & x)                 {m_x = x; }
    X(const X & other)               { *this = other; }
    X & operator = (const X & other) { m_x = other.m_x; return *this; }
    void print()                     { cout << m_x << endl; }
  private :
    int m_x;
};

void main()
{
    X x1(5);
    X x2(4);
    X x3(x2);
    x2 = x1;
    x1.print();
    x2.print();
    x3.print();
}

这个非常简单的示例说明了它是如何完成的 - 相当多。但我注意到在 C# 中似乎并非如此。我必须在 C# 中传递 const 引用吗?我需要“ref”关键字做什么?请注意,我知道并理解什么是 C# 引用和值类型。

In C++, passing const references is a common practice - for instance :

#include <iostream>
using namespace std;

class X
{
  public :
    X()                              {m_x = 0; }
    X(const int & x)                 {m_x = x; }
    X(const X & other)               { *this = other; }
    X & operator = (const X & other) { m_x = other.m_x; return *this; }
    void print()                     { cout << m_x << endl; }
  private :
    int m_x;
};

void main()
{
    X x1(5);
    X x2(4);
    X x3(x2);
    x2 = x1;
    x1.print();
    x2.print();
    x3.print();
}

This very simple example illustrates how it's done - pretty much. However I've noticed that in C# this doesn't seem to be the case. Do I have to pass const references in C# ? what do I need the "ref" keyword for? Please note that I know and understand what C# reference and value types are.

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

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

发布评论

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

评论(4

你穿错了嫁妆 2024-08-10 12:50:09

回答您问题的参考部分;当您将变量传递给方法时,会创建该变量的副本。使用 ref 关键字将传递该变量的相同实例,以便该方法能够为调用者更新该变量。

很多人似乎认为这仅适用于值类型,因为引用类型无论如何都只是作为引用传递,因此 ref 关键字不起作用。然而,事实并非如此,引用以与值相同的方式传递给方法;它被复制并创建该引用的新实例。这意味着调用者将看到对象本身的修改,但看不到引用的修改。因此,如果您尝试将对象设置为 null 或新对象,并且不使用 ref 关键字,调用者将看不到此修改:

不使用 ref:

void UpdatePerson(Person person)
{
   // Caller would see this change
   person.Name = "Bob";
   // Caller wouldn't see this change
   person = null;
}

使用 ref

void UpdatePerson(ref Person person)
{
   // Caller would see this change
   person.Name = "Bob";
   // Caller would see this change
   person = null;
}

To answer the ref part of your question; when you pass a variable to a method, a copy of that variable is created. Using the ref keyword will pass the same instance of that variable so the method would be able to update that variable for the caller.

A lot of people seem to think this only applies to value types because reference types are just passed as a reference anyway so the ref keyword has no effect. However, this isn't true, the reference gets passed to the method in the same way as a value; it's copied and a new instance of the that reference is created. This means that the caller will see modifications to the object itself, but not to the reference. So, if you tried to set the object to null, or a new object, the caller would not see this modification if the ref keyword isn't used:

Without ref:

void UpdatePerson(Person person)
{
   // Caller would see this change
   person.Name = "Bob";
   // Caller wouldn't see this change
   person = null;
}

With ref

void UpdatePerson(ref Person person)
{
   // Caller would see this change
   person.Name = "Bob";
   // Caller would see this change
   person = null;
}
远昼 2024-08-10 12:50:09

C# 没有 const 对象的概念(即不能修改的对象);只有变量(即字段)可以是 const 或 readonly - 这意味着您不能分配给它们。

ref 关键字概念上传递对变量的引用作为参数,即被调用者可以修改变量(而不仅仅是修改值)。这对于原始类型(int、bool 等)特别有用。

C# doesn't have the notion of const objects (i.e. objects which you can't modify); only variables (i.e. fields) can be const or readonly - meaning that you cannot assign to them.

The ref keyword conceptually passes a reference to a variable as the parameter, i.e. the callee can modify the variable (rather than just modifying the value). This is particularly useful for the primitive types (int, bool, etc).

作妖 2024-08-10 12:50:09

从 C# 7.2 开始,可以使用<中的/code> 关键字

Since C# 7.2, this is possible using the in keyword.

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