在 ASP.NET 中使用 C# 全局变量的正确方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10958781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way of using C# global variables in ASP.NET?
提问by Priyank Patel
I am using ASP.NET Web Forms and C# in my application. I have a class file named Global.cs, in which I define variables using setand getproperties. I use those variables anywhere on any pages by instantiating that class object.
我在我的应用程序中使用 ASP.NET Web 窗体和 C#。我有一个名为 的类文件Global.cs,我在其中使用set和get属性定义变量。我通过实例化该类对象在任何页面上的任何位置使用这些变量。
Here is my Global.csfile:
这是我的Global.cs文件:
using System;
using System.Data;
using System.Linq;
using System.Web;
/// <summary>
/// Contains my site's global variables.
/// </summary>
public static class Global
{
/// <summary>
/// Global variable storing important stuff.
/// </summary>
public static string gDate;
public static string gMobLength;
public static string gDateFormat;
public static string gApplicationNo;
public static string gBranchNo;
public static string gMemId;
public static string gIsEditable="false";
public static string gLoggedInUserName;
public static string ImportantData
{
get
{
return gDate;
}
set
{
gDate = value;
}
}
public static string MobileLength
{
get
{
return gMobLength;
}
set
{
gMobLength = value;
}
}
public static string DateFormat
{
get
{
return gDateFormat;
}
set
{
gDateFormat = value;
}
}
public static string ApplicationNo
{
get
{
return gApplicationNo;
}
set
{
gApplicationNo = value;
}
}
public static string BranchNo
{
get
{
return gBranchNo;
}
set
{
gBranchNo = value;
}
}
}
Is this a proper way of using variables throughout the project? What are the possible pros and cons with this approach and what approach would you guys take for using global variables?
这是在整个项目中使用变量的正确方法吗?这种方法可能的优缺点是什么,你们会采用什么方法来使用全局变量?
采纳答案by moribvndvs
First, I'd recommend using autoimplemented properties.
首先,我建议使用自动实现的属性。
public static string BranchNo { get; set; }
Simplifies your code a bit. As to whether or not this is a good approach, it depends. Sometimes simple and straight-forward is better, and this falls into that category. If the values should not change once initialized, you may want to use a proper singleton with initialization:
稍微简化您的代码。至于这是否是一个好方法,这取决于。有时简单明了会更好,这属于这一类。如果初始化后值不应更改,您可能需要使用适当的单例进行初始化:
public class Settings
{
private static Settings _current;
private static readonly object _lock = new object();
public static Settings Current
{
get
{
lock(_lock)
{
if (_current == null) throw new InvalidOperationException("Settings uninitialized");
return _current;
}
}
set
{
if (value == null) throw new ArgumentNullException();
if (_current != null) throw new InvalidOperationException("Current settings can only be set once.");
if (_current == null)
{
lock(_lock)
{
if (_current == null) _current = value;
}
}
}
}
public string ImportantData { get; private set; }
// etc.
}
Initializing settings:
初始化设置:
Settings.Current = new Settings{ ImportantData = "blah blah blah"};
Accessing:
访问:
var data = Settings.Current.ImportantData;
回答by paulsm4
Outside of the two bromides"globals are bad" and "propertiesare good" ... there's nothing intrinsically wrong with your approach. Go for it!
除了两个溴化物“全局不好”和“属性很好”之外……您的方法没有本质上的错误。去吧!
IMHO .. PSM
恕我直言.. PSM

