我在看这个问题,除了枚举某些东西的一种相当奇怪的方式之外,操作遇到了麻烦,因为枚举器是一个结构。我知道返回或传递结构使用副本,因为它是一种值类型:
public MyStruct GetThingButActuallyJustCopyOfIt()
{
return this.myStructField;
}
或者
public void PretendToDoSomething(MyStruct thingy)
{
thingy.desc = "this doesn't work as expected";
}
所以我的问题是,如果 MyStruct 实现了 IMyInterface(如 IEnumerable),这些类型的方法会按预期工作吗?
public struct MyStruct : IMyInterface { ... }
//will caller be able to modify the property of the returned IMyInterface?
public IMyInterface ActuallyStruct() { return (IMyInterface)this.myStruct; }
//will the interface you pass in get its value changed?
public void SetInterfaceProp(IMyInterface thingy)
{
thingy.desc = "the implementing type is a struct";
}