C# 静态类与。具有私有构造函数和所有静态属性和方法的类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/323022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:10:57  来源:igfitidea点击:

Static Class Vs. Class with private constructor and all static properties and methods?

c#.net

提问by Micah

When I create utility classes I typically create a class that has a private constructor and exposes all of it's methods and properties as static. What's the best approach for this? What's the difference between the way I do or creating a static class?

当我创建实用程序类时,我通常会创建一个具有私有构造函数并将其所有方法和属性公开为静态的类。最好的方法是什么?我做的方式或创建静态类的方式有什么区别?

采纳答案by Alan

Static classes are automatically sealed, so people can't inherit and override their behavior.

静态类是自动密封的,所以人们不能继承和覆盖他们的行为。

That is the only real difference (unless there is something special in the IL)

这是唯一真正的区别(除非 IL 中有一些特别的东西)

So if you use a static class, you save yourself the trouble of making the constructor private, and declaring the class sealed.

因此,如果您使用静态类,则可以省去将构造函数设为私有并声明类为密封的麻烦。

I would add, that defining a class as static, is "self-documenting" code. Users of your library will know that this class should not be instantiated, and only has static values.

我想补充一点,将类定义为静态是“自记录”代码。你的库的用户会知道这个类不应该被实例化,并且只有静态值。

回答by Corbin March

A static class can never be instantiated. No way, no how.

静态类永远不能被实例化。没办法,没办法。

A non-static class with a private constructor but all static methods can be abused in various ways - inheritance, reflection, call the private constructor in a static factory - to instantiate the class.

具有私有构造函数但所有静态方法都可以以各种方式滥用的非静态类 - 继承、反射、在静态工厂中调用私有构造函数 - 以实例化类。

If you never want instantiation, I'd go with the static class.

如果你从不想要实例化,我会选择静态类。



Edit - Clarification for FosterZ's comment

编辑 - 澄清 FosterZ 的评论

Say you have this utility class:

假设您有这个实用程序类:

public class Utility
{
    public static string Config1 { get { return "Fourty Two"; } }

    public static int Negate(int x) { return -x; }

    private Utility() { }      
}

If another developer isn't clear on its intent, they could do this:

如果其他开发人员不清楚其意图,他们可以这样做:

public class Utility
{
    public static string Config1 { get { return "Fourty Two"; } }
    public int Config2 { get; set; }

    public static int Negate(int x) { return -x; }

    private Utility() { }

    /// Get an instance of Utility     
    public static Utility GetUtility()
    {
        return new Utility();
    }
}   

Now you have a Frankenstein class. Some of its features require instantiation and some don't. Maybe that's what you want but maybe it's not. You could prevent this with code review, but why not make your intentions explicit in code? Marking the class as staticeliminates any possible confusion. You can't instantiate a static class or inherit from it.

现在你有一个弗兰肯斯坦课程。它的某些功能需要实例化,而有些则不需要。也许这就是你想要的,但也许不是。您可以通过代码审查来防止这种情况发生,但为什么不在代码中明确表达您的意图呢?将类标记为可static消除任何可能的混淆。您不能实例化静态类或从它继承。

回答by Martin

In addition to the previous answers: The compiler won't allow non-static members on static classes and produces and error. This may help a little bit to not accidently add non-static members.

除了前面的答案:编译器不允许静态类上的非静态成员,并且产生和错误。这可能有助于避免意外添加非静态成员。

回答by Massif

Also I'm going to go with the private constructor never being called by the static methods. So any initialisation in there would be wasted... But that's just a theory.

此外,我将使用永远不会被静态方法调用的私有构造函数。所以那里的任何初始化都将被浪费......但这只是一个理论。

回答by Ashutosh Joshi

you can pass object of a class that has private constructor as a parameter to any method but you can't do the same with static class. This is the major difference.

您可以将具有私有构造函数的类的对象作为参数传递给任何方法,但不能对静态类执行相同的操作。这是主要的区别。