运行 时间的 ref 和 out 有何不同?
how ref and out are different at run time?
在他们给出的许多文章中,ref
和 out
在编译时以相同的方式工作 way.even 以相同的方式识别两者,但它们在 运行 时间(CLR)。谁能解释一下它有何不同?和功能?
我在这段代码中尝试了同样的方法
public static void Main()
{
int par = 7;
Program x= new Program();
x.RefMethod(out par);
Console.WriteLine(par);
x.RefMethod1(ref par);
Console.WriteLine(par);
Console.ReadLine();
}
public void RefMethod(out int i)
{
i = 10;
}
public void RefMethod1(ref int i)
{
i = 20;
}
用于引用方法的 ILDASM:
method public hidebysig instance void RefMethod([out] int32& i) cil managed
{
// Code size 6 (0x6)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldc.i4.s 10
IL_0004: stind.i4
IL_0005: ret
} // end of method Program::RefMethod
参考方法 1 的 ILDASM:
method public hidebysig instance void RefMethod1(int32& i) cil managed
{
// Code size 6 (0x6)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldc.i4.s 20
IL_0004: stind.i4
IL_0005: ret
} // end of method Program::RefMethod1
以上清楚地表明两者在一点上是不同的,即在那个方法上 initialising.please 谁能帮我解决我的问题?
请任何人清楚地回答问题。我仍然没有得到答案:(
ref 和 out 之间的区别不在于 CLR,而在于 C# 语言本身。 Ref 和 Out 的不同之处在于它们每个的 "compiler requires different behaviour"。编译时也不允许方法重载。
正如您从代码中看到的,唯一的变化是地址引用特别是这一行(如果您输入值 10,这将增加为 10)。
IL_0002: ldc.i4.s 20
注意:此答案引用了具有误导性的 MSDN 文档,该文档自 been corrected.
是正确的。没有运行时差。
您提到 https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/,其中指出:
Both ref and out are treated differently at run time and they are
treated the same at compile time, so methods cannot be overloaded if
one method takes an argument as ref and the other takes an argument as
an out.
那篇文章的作者可能阅读了 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier ,其中指出:
Although the in, ref, and out keywords cause different run-time
behavior, they are not considered part of the method signature at
compile time. Therefore, methods cannot be overloaded if the only
difference is that one method takes a ref or in argument and the other
takes an out argument.
就我个人而言,我同意 jmoreno,他认为 MSDN 应该使用短语 "require different behavior" 而不是 "cause difference behavior."
您在多处看到这样的解释并不奇怪。可能许多尝试使用 Microsoft 文档作为主要信息来源的作者都提供了相同的误导性解释。
在他们给出的许多文章中,ref
和 out
在编译时以相同的方式工作 way.even 以相同的方式识别两者,但它们在 运行 时间(CLR)。谁能解释一下它有何不同?和功能?
我在这段代码中尝试了同样的方法
public static void Main()
{
int par = 7;
Program x= new Program();
x.RefMethod(out par);
Console.WriteLine(par);
x.RefMethod1(ref par);
Console.WriteLine(par);
Console.ReadLine();
}
public void RefMethod(out int i)
{
i = 10;
}
public void RefMethod1(ref int i)
{
i = 20;
}
用于引用方法的 ILDASM:
method public hidebysig instance void RefMethod([out] int32& i) cil managed
{
// Code size 6 (0x6)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldc.i4.s 10
IL_0004: stind.i4
IL_0005: ret
} // end of method Program::RefMethod
参考方法 1 的 ILDASM:
method public hidebysig instance void RefMethod1(int32& i) cil managed
{
// Code size 6 (0x6)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldc.i4.s 20
IL_0004: stind.i4
IL_0005: ret
} // end of method Program::RefMethod1
以上清楚地表明两者在一点上是不同的,即在那个方法上 initialising.please 谁能帮我解决我的问题?
请任何人清楚地回答问题。我仍然没有得到答案:(
ref 和 out 之间的区别不在于 CLR,而在于 C# 语言本身。 Ref 和 Out 的不同之处在于它们每个的 "compiler requires different behaviour"。编译时也不允许方法重载。
正如您从代码中看到的,唯一的变化是地址引用特别是这一行(如果您输入值 10,这将增加为 10)。
IL_0002: ldc.i4.s 20
注意:此答案引用了具有误导性的 MSDN 文档,该文档自 been corrected.
您提到 https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/,其中指出:
Both ref and out are treated differently at run time and they are treated the same at compile time, so methods cannot be overloaded if one method takes an argument as ref and the other takes an argument as an out.
那篇文章的作者可能阅读了 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier ,其中指出:
Although the in, ref, and out keywords cause different run-time behavior, they are not considered part of the method signature at compile time. Therefore, methods cannot be overloaded if the only difference is that one method takes a ref or in argument and the other takes an out argument.
就我个人而言,我同意 jmoreno,他认为 MSDN 应该使用短语 "require different behavior" 而不是 "cause difference behavior."
您在多处看到这样的解释并不奇怪。可能许多尝试使用 Microsoft 文档作为主要信息来源的作者都提供了相同的误导性解释。