为什么要选择一个静态类在一个单身的实施?静态、单身

2023-09-02 01:33:05 作者:我有自己的专场

的静态与辛格尔顿问题已经很多次在SO讨论。 然而,所有的答案指出了一个单身的许多优点。 我的问题是 - 什么是静态类的优势单身? 为什么不干脆选择一个单身每一次?

The Static Vs. Singleton question has been discussed before many times in SO. However, all the answers pointed out the many advantages of a singleton. My question is - what are the advantages of a static class over a singleton? Why not simply choose a singleton every time?

推荐答案

静态类是你的箱子一个技术手段 - 基本上是一个语言功能

Static class is a technical tool in your box - basically a language feature.

辛格尔顿是一个建筑的概念。

Singleton is an architectural concept.

您可以使用一个静态类,以实现单概念的一种手段。或者你也可以使用其他的方法。

You may use a static class as a means to implement the singleton concept. Or you may use some other approach.

使用静态类在C#中有两个的的潜力的危险,如果你不小心。

With static classes in C# there are two potential dangers if you're not careful.

在所要求的资源不会被释放,直到应用程序的生命结束 在静态变量的值在应用程序中共享。尤其糟糕的ASP.NET应用程序,因为这些值将居住在一个特定的应用领域网站的所有用户之间共享。
 
精彩推荐
图片推荐