Home
C#
static List: Global List Variable
Updated Nov 14, 2022
Dot Net Perls
Static List. A static List is effective for program-wide storage. It references a dynamic array of strings or other data types in only one location in your program.
List, notes. The static List can be put in a separate file in your program for ease of maintenance. Usually, separating classes into separate files is best.
List
An example. This class allows you to store a variable number of elements in a single location. It is essentially a global variable that you can use from anywhere in your program.
static
Note For many programs, this approach is fine. For more complex and large programs, more disciplined approaches are necessary.
Next This code allocates the List in the static constructor. This is run before the List is needed.
Detail You can call the ListTest.Record method to add a string value to the list, and then call Display.
List Add
using System; using System.Collections.Generic; static class ListTest { static List<string> _list; static ListTest() { // Allocate the list. _list = new List<string>(); } public static void Record(string value) { // Record this value in the list. _list.Add(value); } public static void Display() { // Write out the results. foreach (var value in _list) { Console.WriteLine(value); } } } class Program { static void Main() { // Use the static List anywhere. ListTest.Record("AA"); ListTest.Record("BB"); ListTest.Record("CC"); ListTest.Display(); } }
AA BB CC
List references. A List reference can point to a static List. And if we add elements using the local variable, the static List is updated—the references point to the same thing.
var
using System; using System.Collections.Generic; class Program { static List<int> _ids = new List<int>(); static void Main() { var ids = _ids; ids.Add(10); ids.Add(20); // The 2 variables point to the same list. Console.WriteLine(ids.Count); Console.WriteLine(_ids.Count); } }
2 2
Using static instances. There are many ways you can use static instances of collections such as List. Consider a singleton—this is a design pattern surrounding a static instance.
A summary. We used a static List. The term static refers to the single-instance state of the variable, meaning it belongs to the enclosing type and not the object instance.
For data collections that are not dynamic and do not expand, you can use a static array instead of a List. When multiple threads are used, static Lists can be problematic.
static Array
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
No updates found for this page.
Home
Changes
© 2007-2025 Sam Allen