C#Struct通过构造函数赋值
作为代码的一部分,我创建了以下结构:C#Struct通过构造函数赋值,c#,constructor,struct,C#,Constructor,Struct,作为代码的一部分,我创建了以下结构: struct ItemInformation { public int ItemNumber; public double HighPrice, LowPrice, MedianPrice; public ItemInformation(int number, double highP, double lowP) : this() { this.ItemNumber = number; thi
struct ItemInformation
{
public int ItemNumber;
public double HighPrice, LowPrice, MedianPrice;
public ItemInformation(int number, double highP, double lowP) : this()
{
this.ItemNumber = number;
this.HighPrice = highP;
this.LowPrice = lowP;
this.MedianPrice = CalculateMedian(highP, lowP);
}
private double CalculateMedian(double high, double low)
{
return 0.5 * (high + low);
}
}
我知道struct包含四个数据段,但我只想指定三个数据段,因为第四个数据段MedianPrice完全由价格高低决定。所以我想用
ItemInformation jacket;
以及
ItemInformation jacket = new ItemInformation(1234, 145.70, 73.20)
创建该结构的实例,但我似乎无法实现这一点。结构中的所有项都已正确设置,但MedianPrice除外。这可能是一个易变性问题,但我并没有真正改变这里的值。我只是在其他两个值的基础上设置一个值,并且我只想使用三个输入来创建struct的实例。有人对我如何做到这一点有什么建议吗?如有任何建议,将不胜感激。谢谢。我想问题可能是,你指的是:
this.Median
而不是
this.MedianPrice
我想问题可能是,你指的是:
this.Median
而不是
this.MedianPrice
起初
ItemInformation jacket;
并不意味着创建了小于夹套
变量。你不能用它来操作
试试这个:
public ItemInformation(int number = 0, double highP = 0, double lowP = 0)
{
ItemNumber = number;
HighPrice = highP;
LowPrice = lowP;
MedianPrice = 0;
MedianPrice = CalculateMedian(highP, lowP);
}
用法:
ItemInformation jacket1 = new ItemInformation();
ItemInformation jacket2 = new ItemInformation(1234, 145.70, 73.20);
起初
ItemInformation jacket;
并不意味着创建了小于夹套
变量。你不能用它来操作
试试这个:
public ItemInformation(int number = 0, double highP = 0, double lowP = 0)
{
ItemNumber = number;
HighPrice = highP;
LowPrice = lowP;
MedianPrice = 0;
MedianPrice = CalculateMedian(highP, lowP);
}
用法:
ItemInformation jacket1 = new ItemInformation();
ItemInformation jacket2 = new ItemInformation(1234, 145.70, 73.20);
您应该使所有实例字段成为只读的,从而使结构不可变。此外,
MedianPrice
可以是一个属性,并且它不需要struct值中的数据:
struct ItemInformation
{
public readonly int ItemNumber;
public readonly double HighPrice, LowPrice; // no MedianPrice here
public ItemInformation(int number, double highP, double lowP) // no need to chain this()
{
this.ItemNumber = number;
this.HighPrice = highP;
this.LowPrice = lowP;
}
public double MedianPrice
{
get
{
return 0.5 * (HighPrice + LowPrice);
}
}
}
您应该使所有实例字段成为只读的,从而使结构不可变。此外,
MedianPrice
可以是一个属性,并且它不需要struct值中的数据:
struct ItemInformation
{
public readonly int ItemNumber;
public readonly double HighPrice, LowPrice; // no MedianPrice here
public ItemInformation(int number, double highP, double lowP) // no need to chain this()
{
this.ItemNumber = number;
this.HighPrice = highP;
this.LowPrice = lowP;
}
public double MedianPrice
{
get
{
return 0.5 * (HighPrice + LowPrice);
}
}
}
net中的结构通常应符合两种样式之一,我称之为“透明”和“不透明”。请注意,MSDN指南似乎是由一位认为一切行为都像类对象的人编写的,而未能像类对象那样行为是一种缺陷;因此,他们忽略了这样一个事实,即透明结构本身可以具有有用的语义,并且在许多情况下,会使不透明结构效率低下的因素可能会使类更加低效 透明结构是所有字段都公开的结构,并且结构的状态不超过也不低于其字段中的值的组合。透明结构可能具有基于其字段值计算的只读“便利属性”(例如,具有字段DX、DY和DZ的透明
Vector3d
结构可能具有基于这些字段计算的Length
属性)但应该清楚的是,这些属性并不构成结构状态的一部分,而是在字段上执行计算的方便快捷方式。例如,vec.Length
可以作为SomeLibrary.Distance3d(vec.DX、vec.DY、vec.DZ)的替代品
透明结构类型的变量为每个字段分配一个变量;如果需要,可以将每个字段作为其单独的变量进行访问。按值传递透明结构类型的成本与单独传递其所有字段的成本大致相同(在某些情况下,它可能更高效;在其他情况下,效率更低)。通过ref
传递透明结构类型与传递对象引用具有相同的固定成本,而与字段的数量无关
在许多情况下,如果当前版本满足以下所有标准,并且所有可能的未来版本也满足这些标准,则结构应该是透明的[这意味着不满足标准的结构将不会被视为兼容的替代品]
有一些固定的可读成员集(字段或属性),它们公开了它的整个状态
给定这些成员所需的任何值集,可以使用这些值创建实例(不禁止值的组合)。
结构的默认值应该是将所有这些成员设置为各自类型的默认值。
如果结构满足上述条件,公开其字段将不允许外部代码执行其无法执行的任何操作,除非它可能允许快速执行速度较慢的操作,并且可能允许使用原子原语安全地执行线程安全操作,否则需要锁定
当然,并非所有结构都符合上述标准,但如果那些确实符合标准的结构变得透明,性能通常可以得到改善,有时甚至可以得到相当大的改善。事实上,结构通常被认为比类快一点点的原因是,它们的性能因为不必要地使它们不透明而受到阻碍
不透明结构是指其状态在语义上表示字段中的值以外的内容的结构。例如,大小为1234且指数为2的十进制数可能表示数值12.34。不可能只改变不透明结构的一个方面,除非为整个结构计算一个新的值。不透明结构可能试图在其字段上强制使用不变量,但需要注意的是,由于结构赋值的工作方式,在许多情况下,滥用线程(但没有任何特殊的执行权限)的代码是可能的滥用线程以生成违反结构不变量的结构实例
当应用于不透明结构时,微软的指导方针非常好。此类结构通常应避免暴露属性设置器。像Point
和Rectangle
这样的类型并不表示偏离该规则。相反,它们表示的类型一开始就不应该是不透明结构——它们应该是透明结构。在某些情况下,公开属性设置器的不透明结构可以提供通过任何其他方法都无法实现的语义,但是