本文介绍了静态类在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答这个问题(),它让我知道...

In answering this question (http://stackoverflow.com/questions/352317/c-coding-question#352327), it got me wondering...

有没有关于一个静态类等价于一个非静态类instatiation任何危险实现Singleton模式?

Is there any danger in regarding a static class as being equivalent to a non-static class instatiation that implements the singleton pattern?

推荐答案

这似乎是显而易见的,以我的唯一的事情就是一个静态类,基本上是一个集合的范围功能(避免明确办法在这里)和一个单身还是东西,你可以实例,即使你只能有1 1> 0

The only thing that seems immediately apparent to me is that a static class is basically just a collection of scoped functions (explicitly avoiding "methods" here) and a singleton is still something you can instantiate, even if you can only have 1. 1 > 0.

您可以通过一个单身作为一个参数的东西,希望某个接口的对象,就可以不通过静态类的任何地方(除了通过一些反射挂羊头卖狗肉)

You can pass a singleton as an argument to something that expects an object of a certain interface, you cannot pass a static class anywhere (except through some reflection trickery)

这篇关于静态类在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:07