gpt4 book ai didi

c# - IL 约束调用

转载 作者:太空狗 更新时间:2023-10-30 00:40:22 24 4
gpt4 key购买 nike

对于这段代码:

class Program
{
static void Main()
{
Console.WriteLine(new MyStruct().ToString());
}

struct MyStruct { }
}

C# 编译器生成 constrained callvirt IL 代码。

This文章说:

For example, if a value type V overrides the Object.ToString() method, a call V.ToString() instruction is emitted; if it does not, a box instruction and a callvirt Object.ToString() instruction are emitted. A versioning problem can arise <...> if an override is later added.

所以,我的问题是:如果编译器将生成 box 代码而不是约束调用,为什么在这种情况下会出现问题?

最佳答案

box 指令创建了相关实例的副本。允许值类型的实例方法修改调用它们的实例,如果这样做,则在副本上静默调用方法是错误的做法。

static class Program
{
static void Main()
{
var myStruct = new MyStruct();
Console.WriteLine(myStruct.i); // prints 0
Console.WriteLine(myStruct.ToString()); // modifies myStruct, not a copy of myStruct
Console.WriteLine(myStruct.i); // prints 1
}

struct MyStruct {
public int i;
public override string ToString() {
i = 1;
return base.ToString();
}
}
}

关于c# - IL 约束调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28395401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com