为什么要选择一个静态类在一个单身的实施? [英] Why choose a static class over a singleton implementation?

查看:199
本文介绍了为什么要选择一个静态类在一个单身的实施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的静态与辛格尔顿问题已经很多次在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应用程序,因为这些值将居住在一个特定的应用领域网站的所有用户之间共享。

这篇关于为什么要选择一个静态类在一个单身的实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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