gpt4 book ai didi

c# - 有关结构实现接口(interface)时发生的情况的详细信息

转载 作者:可可西里 更新时间:2023-11-01 08:08:21 25 4
gpt4 key购买 nike

我最近遇到了这个 Stackoverflow 问题:When to use struct?

在里面,它有一个答案,说的有点深奥:

In addition, realize that when a struct implements an interface - as Enumerator does - and is cast to that implemented type, the struct becomes a reference type and is moved to the heap. Internal to the Dictionary class, Enumerator is still a value type. However, as soon as a method calls GetEnumerator(), a reference-type IEnumerator is returned.

这到底是什么意思?

如果我有类似的东西

struct Foo : IFoo 
{
public int Foobar;
}

class Bar
{
public IFoo Biz{get; set;} //assume this is Foo
}

...

var b=new Bar();
var f=b.Biz;
f.Foobar=123; //What would happen here
b.Biz.Foobar=567; //would this overwrite the above, or would it have no effect?
b.Biz=new Foo(); //and here!?

值类型结构被视为引用类型的具体语义是什么?

最佳答案

结构类型的每个声明实际上在运行时声明了两种类型:值类型和堆对象类型。从外部代码的角度来看,堆对象类型将表现得像一个具有相应值类型的字段和方法的类。从内部代码的角度来看,堆类型的行为就像它具有相应值类型的字段 this

尝试将值类型转换为引用类型(ObjectValueTypeEnum 或任何接口(interface)类型)将生成一个新的其对应的堆对象类型的实例,并返回对该新实例的引用。如果试图将值类型存储到引用类型存储位置,或将其作为引用类型参数传递,也会发生同样的事情。一旦该值被转换为堆对象,从外部代码的角度来看,它将表现为堆对象。

唯一可以在不首先将值类型转换为堆对象的情况下使用值类型的接口(interface)实现的情况是,当它作为具有接口(interface)类型作为约束的泛型类型参数传递时。在那种特定情况下,接口(interface)成员可以用在值类型实例上,而不必先将其转换为堆对象。

关于c# - 有关结构实现接口(interface)时发生的情况的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15207260/

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