static
修饰符意味着该类型无法实例化,或者成员无法与实例关联。但是这种对实例化的限制有什么好处?在什么用例场景下应该将类型/成员声明为 static ?
最佳答案
But whats the benefit of this restriction on instantiation and under what use case scenario should types/members be declared static ?
对于成员,当您拥有属于该类的状态(或应在该类的所有实例之间共享)或不依赖于实例级状态的方法时。
对于类型,当您有一组不依赖于实例级状态的方法(例如,System.Math
)(包括扩展方法)时。
关于c# - 何时应将类型或成员声明为静态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3968761/