【发布时间】:2023-04-02 01:09:02
【问题描述】:
最近一个编译器警告和(非常有用的)提示提示我编写下面的代码。
我不知道你可以这样做,但这是完全合法的,而且也很方便,因为我可以声明一个具有类似于非托管结构的公共字段的公共属性的托管结构,并且还可以用一个对象而不是必须将所有字段作为参数传递。
让我困惑的是,这似乎调用了显式的无参数构造函数,这对于这个结构当然是非法的。
这是怎么回事,这种语法是否一直受到支持?
internal struct IconEntry
{
public byte Width { get; set; }
public byte Height { get; set; }
public byte ColorCount { get; set; }
public byte Reserved { get; set; }
public short Planes { get; set; }
public short BitCount { get; set; }
public int BytesInRes { get; set; }
public int ImageOffset { get; set; }
public IconEntry(BinaryReader reader)
: this()
{
Width = reader.ReadByte();
Height = reader.ReadByte();
ColorCount = reader.ReadByte();
Reserved = reader.ReadByte();
Planes = reader.ReadInt16();
BitCount = reader.ReadInt16();
BytesInRes = reader.ReadInt32();
ImageOffset = reader.ReadInt32();
}
}
【问题讨论】:
标签: c# .net constructor struct automatic-properties