当使用静态变量? [英] When to use static variables?

查看:123
本文介绍了当使用静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在做C#项目有很多的渲染,并在整个几乎所有的班级有被用于渲染的缩放整数类型的恒定值。我知道我可以在一个地方作为一个普通变量定义这个常量,然后通过周围,但是,这个seemes真的很麻烦。什么时候可以接受的使用静态变量在C#中?我的问题最简单的解决方案是创建一个包含所有其他类可以引用静态变量的类 - 那会是糟糕的设计。


解决方案

没有设计不错。事实上,有一个公开的静态方法和静态价值的共同或实用程序命名空间和类集中在一个地方这些值,这样可以保证在您的应用程序的每个模块使用适当的值。它的低凝聚力,但对于受益接受。我看到它没有问题。


I'm currently doing a project in C# with a lot of rendering, and throughout almost all the classes there's a constant value of the type integer being used for scaling of the rendering. I know I could define this constant in one place as a normal variable and then pass it around, but this seemes really cumbersome. When is it acceptable to use static variables in C#? The easiest solution to my problem would be to create a class containing the static variable that all the other classes could reference - would that be bad design?

解决方案

Not bad design at all. In fact, having a Common or Utility namespace and class that exposes static methods and static values centralizes these values in one place so you can ensure that every module in you application is using the appropriate values. It's low cohesion, but acceptable for the benefit. I see no problem with it.

这篇关于当使用静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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