Ras*_*sto 2 .net c# performance inheritance constructor
在我的应用程序中,我创建了大量的"小"类实例,只保存了一些数据.
我知道类创建(即构造函数调用)成本很高.我的问题是:如果我让这个小类继承自另一个类,会不会更昂贵?有些字段只会移动到超类,所以基本上我可以使用
注意:此问题专门针对.NET中的性能.
考虑这两种情况:
1.案件只包括一个班级.这是原来的课程:
public class ShapeWithOffset{
public double XOffset { get; private set; }
public double YOffset { get; private set; }
public IShape Shape{ get; private set; }
public ShapeWithOffset(double xOffset, double yOffset, IShape shape){
//check if arguments are correct/throw ArgumentException if not
XOffset = xOffset;
YOffset = yOffset;
Shape = shape;
}
//equality members
}
Run Code Online (Sandbox Code Playgroud)
2. case由2个类组成,其中second继承自first.第二类ShapeWithHorizontalAndVerticalOffset提供与ShapeWithOffset1. case中相同的功能.
public class ShapeWithHorizontalOffset{
public double XOffset { get; private set; }
public IShape Shape { get; private set; }
public ShapeWithHorizontalOffset(double xOffset, IShape shape){
//check if arguments are correct/throw ArgumentException if not
XOffset = xOffset;
Shape = shape;
}
//equality members
}
public class ShapeWithHorizontalAndVerticalOffset : ShapeWithHorizontalOffset{
public double YOffset { get; private set; }
public ShapeWithHorizontalAndVerticalOffset(double xOffset, double yOffset,
IShape shape) : base(xOffset, shape){
//check if yOffset is correct/throw ArgumentOutOfRangeException if not
Yffset = yOffset;
}
//equality members
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:命令var foo = new ShapeWithOffset(20, 10, shape);更快
var boo = new ShapeWithHorizontalAndVerticalOffset(20, 10, shape);吗?
如果我使用组合而不是继承,那将是......但是继承呢?
| 归档时间: |
|
| 查看次数: |
299 次 |
| 最近记录: |