응용 프로그램에서 ASP.NET Web Forms 및 C#을 사용하고 있습니다. 나는 이라는 이름의 클래스 파일을 가지고 있는데, 여기서 나는 set
과 get
속성을 사용하여 변수를 정의한다. 그 클래스 객체를 인스턴스화하여 페이지의 아무 곳에 나 변수를 사용합니다.ASP.NET에서 C# 전역 변수를 사용하는 올바른 방법은 무엇입니까?
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;
}
}
}
이 프로젝트를 통해 변수를 사용하는 적절한 방법인가 : 내 Global.cs
파일은 여기에
입니까? 이 접근법의 장단점은 무엇이며 전역 변수를 사용하기 위해 어떤 접근 방식을 취하고 있습니까?
그래도이 스레드는 안전합니까? – McGarnagle
@HackedByChinese 제안을 주셔서 감사합니다. 내 접근 방식과 내 코드를 단순화하기 위해 제안한 접근 방식의 차이점은 무엇입니까? 감사합니다. – freebird
자동 구현 된 속성은 자동으로 뒷받침 필드를 생성하므로 소스에 포함하지 않아도됩니다. – HackedByChinese