“ref return usage details” 可以翻译为 “ref返回用法细节”。

huangapple go评论57阅读模式
英文:

ref return usage details

问题

The difference in behavior between ref string author4 and string author4 in this context is related to how C# handles references and value types.

When you declare ref string author4, you are creating a reference to the actual string in the authors array. So when you modify author4, you are directly modifying the string in the array, which is why it affects the authors array.

On the other hand, when you declare string author4, you are creating a new string variable that initially points to the same string in the array. However, when you assign a new value to author4, you are creating a new string object in memory, and the variable author4 now points to this new string. It no longer references the original string in the authors array. Therefore, modifying author4 does not affect the authors array.

In summary, the difference lies in whether you are modifying the original string in the array (with ref) or creating a new string (without ref) when you assign a different value to author4.

英文:
public ref string FindAuthor(int number, string[] names)
{
    if (names.Length > 0)
        return ref names[number];   
    throw new IndexOutOfRangeException($"{nameof(number)} not found.");
}
//main method
string[] authors = { "Mahesh Chand", "Mike Gold", "Dave McCarter", 
                "Allen O'neill", "Raj Kumar" };

//first variation
string author4 = new Program().FindAuthor(3, authors);
Console.WriteLine("Original author:{0}", author4);
author4 = "Chris Sells";
Console.WriteLine("Replaced author:{0}", authors[3]);

//secondvariation
ref string author4 = ref new Program().FindAuthor(3, authors);
Console.WriteLine("Original author:{0}", author4);
author4 = "Chris Sells";
Console.WriteLine("Replaced author:{0}", authors[3]);

So the FindAuthor returns by reference and when I capture that in ref string author4 and then modify it, the authors array changes as expected.

But when I say string author4 and then modify the author4 then the authors does not change but it kind of does not make sense to me since the FindAiythor method always returns by reference so shouldn't author4 always change the reference?

Could you please tell me what happens here (under the hood maybe)?

答案1

得分: 4

当你使用 string author4 = {expr} 而不是 ref string author4 = ref {expr} 时,该方法返回对字符串引用的引用,并且编译器立即取消引用它,仅将字符串引用(而不是对字符串引用的引用)存储在本地。在那一点上:对 author4 的所有更改只是更改本地变量,因此不会更新 authors。在完整的 ref 版本中,对 author4 的赋值是一次间接写入;本地的值从未改变;相反,该值用于确定实际写入的位置。

实际上,这似乎是一种不寻常的 ref 返回选择。

英文:

When you say string author4 = {expr} instead of ref string author4 = ref {expr}, the method returns a reference to the string-reference, and the compiler immediately de-references that, storing just the string-reference (not the reference to the string-reference) in the local. At that point: all changes to author4 are just changing the local variable, hence no update to authors. With the full ref version, the assignment to author4 is an indirect write; the value of the local is never changed; instead the value is used to determine where to do the actual write.

In reality, this seems like an unusual choice for a ref return.

huangapple
  • 本文由 发表于 2023年4月13日 20:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定