Keywords: C# | Static Variables | Cross-Class Access
Abstract: This article provides an in-depth exploration of how to access static variables from one public class to another in C#. By analyzing two primary approaches—direct access to static fields and encapsulation through properties—it details implementation steps, applicable scenarios, and their respective advantages and disadvantages. Based on practical code examples, the article explains the straightforward access method using public static fields and contrasts it with the enhanced data security and flexibility offered by property encapsulation. It also discusses specific applications in console applications, assisting developers in selecting appropriate data sharing solutions based on project requirements.
Fundamentals of Cross-Class Static Variable Access
In C# object-oriented programming, data sharing between classes is a common requirement. When needing to access the same data across different classes, static variables offer an effective solution. Static variables belong to the class itself rather than instances of the class, meaning they can be accessed directly without creating an object of the class.
Consider the basic implementation: defining a static string variable name in the Variables class and making it visible externally through the public access modifier. The corresponding code structure is as follows:
public class Variables
{
public static string name = "";
}In the Main class, the static variable can be directly referenced via the class name:
public class Main
{
public void AccessVariable()
{
Console.WriteLine(Variables.name);
}
}The advantage of this method lies in its simplicity and clarity, requiring only a single line of code to perform the access. However, exposing the field directly as public may introduce data security risks, as external code can freely modify its value.
Advanced Implementation with Property Encapsulation
To enhance code robustness and maintainability, it is recommended to encapsulate static variables using properties. This approach controls data access through get and set accessors, allowing validation logic to be added during assignment.
Improved implementation of the Variables class:
public class Variables
{
private static string name = "";
public static string Name
{
get { return name; }
set { name = value; }
}
}The access method in the Main class becomes:
public class Main
{
public void DoSomething()
{
string currentName = Variables.Name;
Console.WriteLine(currentName);
}
}Although property encapsulation adds a small amount of code, it provides better data control capabilities. For instance, length checks or format validation can be incorporated into the set accessor to ensure data validity.
Analysis of Practical Application Scenarios
In console applications, both methods work effectively. Direct access to static fields is suitable for rapid prototyping or internal tool projects, while property encapsulation is more appropriate for large-scale projects or scenarios requiring strict data control.
When choosing a solution, factors such as project scale, team collaboration needs, and data security requirements should be considered. For most production environments, property encapsulation is recommended to ensure long-term code maintainability.