C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
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.
Example. 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.
C# program that uses static string using System; class Program { 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 ---"); Console.WriteLine(_example); // // Call this method. // Read(); } static void Read() { // // Display value of string. // Console.WriteLine("--- Value, Read method ---"); Console.WriteLine(_example); } } Output String is null --- Value, Main method --- 1000 --- Value, Read method --- 1000
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. This is possible with instance and static strings and other types.
We assign static strings with the assignment operator. This is a bitwise copy of the reference value. When you assign the value of ToString(), data is allocated on the managed heap. The reference now points to that object data.
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.
Const strings 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.
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.
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.
Finally: We compared it to const and readonly strings, as well as static methods. Static strings are common in programs.