我的问题与静态方法和实例方法的性能特征及其可扩展性有关。假设在此场景中,所有类定义都在单个程序集中,并且需要多个离散指针类型。
考虑以下情况:
public sealed class InstanceClass
{
public int DoOperation1(string input)
{
// Some operation.
}
public int DoOperation2(string input)
{
// Some operation.
}
// … more instance methods.
}
public static class StaticClass
{
public static int DoOperation1(string input)
{
// Some operation.
}
public static int DoOperation2(string input)
{
// Some operation.
}
// … more static methods.
}
以上类代表了一个辅助样式模式。
在实例化的类中,解决实例方法需要花费一些时间,而与 StaticClass 相反。
我的问题是:
当状态不是关注点(没有需要的字段或属性)时,始终使用静态类是否更好?
如果有大量这些静态类定义(例如说100个,每个有若干个静态方法),与同样数量的实例类定义相比,这会对执行性能或内存消耗产生负面影响吗?
当调用同一实例类中的另一个方法时,实例化解析是否仍然存在?例如,在同一实例中从
DoOperation1
中使用 [this] 关键字像this.DoOperation2("abc")
一样。
this
指向某个东西的操作?” - Jon Hanna