在 .NET 中,如果将结构传递到带有接口参数的方法中,它是否会将值装箱?
从一个简单的测试中我可以看到,如果您将结构传递到方法中,它会按值传递,但如果您首先将它分配给接口,它会按引用传递。
interface IFoo { int Val { get; set; } }
struct Foo : IFoo { public int Val { get; set; } }
void Bar(IFoo foo) { foo.Val = 1; }
Foo foo = new Foo();
IFoo ifoo = new Foo();
Bar(foo);
Bar(ifoo);
Console.WriteLine(foo.Val); // 0, passed by value
Console.WriteLine(ifoo.Val); // 1, passed by ref
所以我的问题是,是否还有像这样传递结构的装箱操作?
From a simple test I can see that if you pass the struct into the method it is passed by value but if you first assign it to an interface it is passed by reference.
interface IFoo { int Val { get; set; } }
struct Foo : IFoo { public int Val { get; set; } }
void Bar(IFoo foo) { foo.Val = 1; }
Foo foo = new Foo();
IFoo ifoo = new Foo();
Bar(foo);
Bar(ifoo);
Console.WriteLine(foo.Val); // 0, passed by value
Console.WriteLine(ifoo.Val); // 1, passed by ref
So my question is, is there still a boxing operation for passing a struct in like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当结构体转换为接口时,它就会被装箱。
您可以通过使用接口作为约束使参数泛型来避免装箱:
这利用了泛型针对每个值类型进行专门化的事实。
但是,如果您遵循可变结构是邪恶的设计准则,从而使您的结构不可变,那么代码框与否并不重要。装箱只会对性能造成轻微影响,但不会对语义产生太大影响。
Whenever a struct gets converted to an interface it gets boxed.
You can avoid that boxing by making the parameter generic using the interface as a constraint:
This uses the fact that generics get specialized for every value type.
But if you follow the design guideline that mutable strucs are evil, and thus make your struct immutable, it doesn't matter much if the code boxes or not. The boxing then only causes a slight performance hit, but doesn't change semantics much.
接口都是引用类型,因此装箱发生在线路上,
而不是调用
Bar
时。Interfaces are all reference types, so the boxing occurs on the line
rather than when calling
Bar
.