有几种存储静态变量的方法?您更喜欢哪一个? [英] How many ways to store static variables? And which one do you prefer?

查看:111
本文介绍了有几种存储静态变量的方法?您更喜欢哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#语言中,选择了几种方法来存储静态变量,或在整个(.sln)文件中存储诸如静态vars之类的内容?

In C# language,how many ways is chosed to store static variables, or something like static vars in the whole (.sln ) file?

And which one do you prefer?

推荐答案

根据我的喜好,
.
C#是一种面向对象的语言,所有内容都是基于类的.它没有要描述的全局"变量的概念.但是,在某些情况下,最好使用单个应用程序范围的变量-例如,可以从配置文件中读取一次到数据库的连接字符串,并将其存储在静态变量中.像这样的案例非常非常少-否则您的设计有一些错误.

发生这种情况时,我通常会构造一个静态类,并使用该类包含静态变量,以及任何与类无关的方法(这种方法同样很少见).
By preference, I don''t.
C# is a object oriented language, and everything is class based. It has no concept of "global" variables, which is what you are describing. However, there are occasions when a single application-wide variable is a good idea - the connection string to a database for example, could be read from a config file once, and stored in a static variable. Cases like this are very, very rare though - or there is something very wrong with your design.

When that happens, I generally construct a static class, and use that to contain the static variable, along with any non-class related methods (which again are as rare as possible).


您应该已经在Google上搜索了.以下应该可以为您提供帮助.

静态变量 [
You should have searched google for it. Following should help you.

Static Variables[^]


这篇关于有几种存储静态变量的方法?您更喜欢哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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