Apologies for not using a comment, I don t think it will fit.
Jon, this is not an actual implementation, I m just trying to get a deeper understanding of structs, so no worries about me implementing mutable structs :)
无论如何,我不相信你是正确的。 考虑这一法典与第一个例子几乎相同:
public struct SomeStruct
页: 1
public int PublicProperty 页: 1 get; set; iii
public int PublicField;
public SomeStruct(int propertyValue, int fieldValue)
: this()
页: 1
PublicProperty = propertyValue;
PublicField = fieldValue;
iii
public int GetProperty()
页: 1
return PublicProperty;
iii
public void SetProperty(int value)
页: 1
PublicProperty = value;
iii
iii
class Program
页: 1
static void Main(string[] args)
页: 1
SomeStruct a = new SomeStruct(1, 1);
a.PublicProperty++;
a.SetProperty(a.GetProperty()+1);
iii
iii
现在,用弹道看sil,主要方法如下:
私人隐蔽的方法
页: 1
.entrypoint
// Code size 45 (0x2d)
.maxstack 3
.locals init ([0] valuetype ConsoleApplication1.SomeStruct a)
IL_0000: nop
IL_0001: ldloca.s a
IL_0003: ldc.i4.1
IL_0004: ldc.i4.1
IL_0005: call instance void ConsoleApplication1.SomeStruct::.ctor(int32,
int32)
IL_000a: nop
IL_000b: ldloca.s a
IL_000d: dup
IL_000e: call instance int32
ConsoleApplication1.SomeStruct:get_Publicproperty()
IL_0013: ldc.i4.1
IL_0014: add
IL_0015: call instance void
ConsoleApplication1.SomeStruct:set_PublicProperty(int32)
IL_001a: nop
IL_001b: ldloca.s a
IL_001d: ldloca.s a
IL_001f: call instance int32 ConsoleApplication1.SomeStruct::GetProperty()
IL_0024: ldc.i4.1
IL_0025: add
IL_0026: call instance void ConsoleApplication1.SomeStruct::SetProperty(int32)
IL_002b: nop
IL_002c: ret
iii
我对可怕的格局表示歉意,我不敢肯定如何使这种安排正常。 无论如何,希望你们能够看到,主要方法中最后两条守则实际上相同。
因此,我想,从前一个职位来看,这一行文如下:
a.OtherStruct.PublicProperty++;
实际与后线相同:
a.OtherStruct.SetProperty(a.OtherStruct.GetProperty() + 1);
因此,在我看来,第一行并不仅仅因为汇编者不支持它而汇编成册,而并非因为其不合法。
你认为什么?