以下是MSDN在“何时使用静态类”一节中所说的:

static class CompanyInfo { public static string GetCompanyName() { return "CompanyName"; } public static string GetCompanyAddress() { return "CompanyAddress"; } //... } Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.

对我来说,这个例子似乎没有涵盖静态类的很多可能的使用场景。在过去,我曾将静态类用于相关函数的无状态套件,但仅此而已。那么,在什么情况下应该(和不应该)将类声明为静态的呢?


当前回答

这是自面向对象开发开始以来另一个古老但非常热门的问题。 当然,使用(或不使用)静态类的原因有很多,其中大多数已经在大量的答案中涵盖了。

我想说的是,我让一个类是静态的,当这个类在系统中是唯一的并且在程序中有它的实例是没有意义的。但是,我将这种用法保留给大型类使用。我从来没有将MSDN示例中这样的小类声明为“静态的”,当然,也没有将成为其他类成员的类。

我还想指出,静态方法和静态类是两种不同的东西。在已接受的答案中提到的主要缺点是针对静态方法。静态类提供了与普通类相同的灵活性(涉及属性和参数),其中使用的所有方法都应该与类存在的目的相关。

在我看来,静态类候选的一个很好的例子是“FileProcessing”类,它将包含与程序的各种对象相关的所有方法和属性,以执行复杂的FileProcessing操作。拥有这个类的多个实例几乎没有任何意义,而静态将使程序中的任何东西都可以随时使用它。

其他回答

对于c# 3.0,扩展方法可能只存在于顶级静态类中。

我只对辅助方法使用静态类,但随着c# 3.0的出现,我更愿意为这些方法使用扩展方法。

我很少使用静态类方法,原因与我很少使用单例“设计模式”相同。

When deciding whether to make a class static or non-static you need to look at what information you are trying to represent. This entails a more 'bottom-up' style of programming where you focus on the data you are representing first. Is the class you are writing a real-world object like a rock, or a chair? These things are physical and have physical attributes such as color, weight which tells you that you may want to instantiate multiple objects with different properties. I may want a black chair AND a red chair at the same time. If you ever need two configurations at the same time then you instantly know you will want to instantiate it as an object so each object can be unique and exist at the same time.

On the other end, static functions tend to lend more to actions which do not belong to a real-world object or an object that you can easily represent. Remember that C#'s predecessors are C++ and C where you can just define global functions that do not exist in a class. This lends more to 'top-down' programming. Static methods can be used for these cases where it doesn't make sense that an 'object' performs the task. By forcing you to use classes this just makes it easier to group related functionality which helps you create more maintainable code.

大多数类既可以用静态的也可以用非静态的来表示,但是当你有疑问时,只要回到OOP的根源,试着想想你在表示什么。这是一个正在执行动作的对象(一辆可以加速、减速、转弯的汽车)还是更抽象的对象(比如显示输出)?

与你的内部OOP保持联系,你就永远不会出错!

这是自面向对象开发开始以来另一个古老但非常热门的问题。 当然,使用(或不使用)静态类的原因有很多,其中大多数已经在大量的答案中涵盖了。

我想说的是,我让一个类是静态的,当这个类在系统中是唯一的并且在程序中有它的实例是没有意义的。但是,我将这种用法保留给大型类使用。我从来没有将MSDN示例中这样的小类声明为“静态的”,当然,也没有将成为其他类成员的类。

我还想指出,静态方法和静态类是两种不同的东西。在已接受的答案中提到的主要缺点是针对静态方法。静态类提供了与普通类相同的灵活性(涉及属性和参数),其中使用的所有方法都应该与类存在的目的相关。

在我看来,静态类候选的一个很好的例子是“FileProcessing”类,它将包含与程序的各种对象相关的所有方法和属性,以执行复杂的FileProcessing操作。拥有这个类的多个实例几乎没有任何意义,而静态将使程序中的任何东西都可以随时使用它。

静态类非常有用,有一席之地,例如库。

我能提供的最好的例子是. net Math类,它是一个包含数学函数库的系统名称空间静态类。

这就像任何其他事情一样,使用正确的工具来完成工作,如果没有的话,任何工具都可以滥用。

盲目地认为静态类是错误的,不要使用它们,或者说“只能有一个”或没有,都是错误的,就像过度使用它们一样。

c#。Net包含许多静态类,就像Math类一样。

因此,只要正确实现,它们就非常有用。

我们有一个静态TimeZone类,它包含许多业务相关的时区函数,不需要像Math类那样创建多个类实例,它在静态类中包含一组全局可访问的TimeZone相关函数(方法)。