C# Static StringThis C# article shows how to use static string fields. It discusses static string allocation.
A static string is stored in one location. It has many differences from regular strings, such as instance strings and local variables. The string is tied to the type declaration rather than the object instance.StaticStrings
Info: The static keyword in the C# language was not chosen for its meaning. It was instead chosen because it was used in C++ and C.
When you use the static keyword on a string, you indicate that you only need one string reference, which can point to only one object. If you have many string values in your program, don't choose the static keyword.
Info: The static string is initialized to null by having all its bits set to zero. We check the value of the static string before it is assigned.Null
Assign: We assign static strings with the assignment operator. This is a bitwise copy of the reference value.Assign Variables
Note: When you assign the value of ToString(), data is allocated on the managed heap. The reference now points to that object data.ToString
C# program that uses static string
static string _example;
static void Main()
// Check initial value of static string.
if (_example == null)
Console.WriteLine("String is null");
// Assign static string to value.
_example = 1000.ToString();
// Display value of string.
Console.WriteLine("--- Value, Main method ---");
// Call this method.
static void Read()
// Display value of string.
Console.WriteLine("--- Value, Read method ---");
String is null
--- Value, Main method ---
--- Value, Read method ---
Static and instance strings
can be used in other methods on the same type. Static strings can be used by all instance and static methods on the type. The Read method in the program is static. It can access the string reference.
And: If it was to change the value of the reference, the static string would also be changed in Main.
Discussion. We discuss differences between static strings and readonly and const strings. Static strings can be assigned as many times as you want in your program. This is different from const strings, which must be assigned to a constant value.
can be accessed with the same syntax as static strings. Finally, readonly strings can be instance strings or static strings, and can only be assigned in a constructor.Readonlyconst
The static keyword
can be applied to invocable members. It can also be applied to the class keyword. A static member variable such as a static string is still a field, while a static method is a function type.
Tip: The static keyword does not affect the usage of the type, only that it exists only once, regardless of the number of instances.Singleton, Static Class
Summary. We saw an example of a static string in the C# language. We described its usage and how it points to only one storage location in your program. We noted that it does not rely on an instance of any class.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.