Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在`System.Windows.Forms`控件中没有`ref`关键字时,按引用传递是如何工作的_C#_.net_Winforms_Pass By Reference_Ref - Fatal编程技术网

C# 在`System.Windows.Forms`控件中没有`ref`关键字时,按引用传递是如何工作的

C# 在`System.Windows.Forms`控件中没有`ref`关键字时,按引用传递是如何工作的,c#,.net,winforms,pass-by-reference,ref,C#,.net,Winforms,Pass By Reference,Ref,我想知道在System.Windows.Forms控件中没有ref关键字的情况下,通过引用传递是如何工作的 这是我迄今为止尝试过的代码 ChangeProperties(button1); //Call the method without ref keyword 这里是方法 public void ChangeProperties(Button btn) { // Code that changes the properties } 当我在不使用ref关键字的情况下通过传递按钮调用上述

我想知道在
System.Windows.Forms
控件中没有
ref
关键字的情况下,通过引用传递是如何工作的

这是我迄今为止尝试过的代码

ChangeProperties(button1); //Call the method without ref keyword
这里是方法

public void ChangeProperties(Button btn)
{
  // Code that changes the properties
}
当我在不使用
ref
关键字的情况下通过传递按钮调用上述方法时,原始按钮的属性会不断更改

我还用
int

int j=10;
ChangeValue(j);// Value doesn't change without ref keyword
下面是方法

public static int ChangeValue(int i)
{
    i = 0;
    return i;
}

有人能告诉我这是怎么发生的吗

按钮
是一种引用类型,因此即使您没有通过引用传递,您也在通过一个引用(通过值)。换句话说,对象的副本不是创建的,一个“指针”(使用C++术语)传递给函数。 因此,当您更改其属性时,这些更改将反映在原始对象中。请注意,分配参数不会反映在调用方法中

int
是一种值类型,因此在传递之前会进行复制(除非通过
ref
传递)。因此,更改(分配)不会传播回调用方

要澄清评论中的答复:


不能按值传递引用类型对象,这意味着运行时必须在传递引用类型对象之前创建一个副本。但是,引用本身是通过值传递的(这就是为什么赋值不会传播到调用方)。还可以使用
Object.MemberwiseClone
(和类似方法)显式创建副本,并按值传递引用。但是,该函数特别容易复制,所以在使用它时要注意这一点。

有一篇关于这方面的好文章,让我试着把它挖掘出来。这里是:虽然我没有要求问这个问题,但出于好奇,我在问-我可以在调用按钮的方法时将passby引用更改为passby值(比如int)吗@Jeffrey Richter的《BradleyDotNETCLR via C#》一书帮助我理解了许多与.NET类似的问题。在这本书中,在前几章中解释ref关键字时,它有一个示例来演示如何通过ref(使用ref关键字)传递引用类型,这非常令人印象深刻。