C# Static String Usage

You want to find information about static strings in the C# programming language. Static strings have many differences with regular strings, such as instance strings and local variables. They are tied to the type declaration rather than the object instance. Here we look at an example of using static strings in a C# program, and then discuss some of their advantages and compare them to other kinds of strings.

Using static string

First, the static keyword in the C# language was not chosen for its meaning, but was instead chosen because it was used extensively 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 at a time. If you have many string values in your program, don't choose the static keyword.

~~~ Program that uses static string (C#) ~~~

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 of the program ~~~

String is null
--- Value, Main method ---
1000
--- Value, Read method ---
1000

Initial value of static string. The static string _example here is initialized to null by having all its bits set to zero. The Main entry point checks the value of the static string before it is assigned in the program text. This is possible with instance and static strings and other types.

Assigning static string. You can assign static strings such as _example using the assignment operator. The assignment always performs a bitwise copy of the reference value. When you assign the value of ToString() in the example, the string characters are allocated on the managed heap, and the static string reference now points to that object data.

Using static string in another method. Static and instance strings can be used in other methods on the same type. Static strings can be used by all instance and all static methods on the type. The Read method in the program text is static, and it can access the string reference. If it was to change the value of the reference, the static string would also be changed in Main.

Comparing const and readonly strings

Here we discuss some 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 than const strings, which must be assigned to a constant value (such as "Dot Net Perls"). 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.

See Readonly Field Usage.

Understanding static keyword

The static keyword can be applied to invocable members in the C# programming language. It can also be applied to the class keyword, but not the namespace keyword. However, a static member variable such as a static string is still a field, while a static method is a function type. The static keyword does not affect the usage of the type, only that it exists only once, regardless of the number of instances. More information on statics can be found on this site.

See Singleton Pattern Versus Static Class.

Summary

Here we saw an example of a static string in the C# programming language and 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 and are very useful when used correctly.

See Static Overview.

See String Overview.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen