静态类比。与私有构造函数和所有的静态属性和方法的类? [英] Static Class Vs. Class with private constructor and all static properties and methods?

查看:160
本文介绍了静态类比。与私有构造函数和所有的静态属性和方法的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建的实用工具类我通常创建一个具有私有构造函数,并公开它的所有方法和属性为静态的类。什么是我们的最佳方法呢?什么是我的方式,或创建一个静态类?之间的差异

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?

推荐答案

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

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

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

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

所以,如果你使用一个静态类,你救自己制作的构造私人,并宣布了密封类的麻烦。

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

我想补充一点,定义一个类为静态的,是自文档code。库的用户会知道,这个类不应该被实例化,只有静态值。

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.

这篇关于静态类比。与私有构造函数和所有的静态属性和方法的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆