如何在 C# 中传递常量引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1385224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:54:37  来源:igfitidea点击:

How do I pass a const reference in C#?

c#referenceconst

提问by Maciek

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

在 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();
}

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.

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

采纳答案by Martin v. L?wis

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.

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

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).

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

回答by Charlie

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.

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

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 referencegets passed to the method in the same way as a value; it's copied and a new instance of the that referenceis 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:

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

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;
}

回答by Frederik Gheysels

Since C# 7.2, this is possible using the inkeyword.

从 C# 7.2 开始,可以使用in关键字.