TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Static List Example

This C# program uses a static List instance. Static lists are a useful field type.

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. It can be put in a separate file in your program for ease of maintenance.

Example. First, the class shown below 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. For many programs, this approach is fine.

However: 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.

Static Constructor

Program containing static List: C#

using System;
using System.Collections.Generic;

static class ListTest
{
    static List<string> _list; // Static List instance

    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);
	}
    }
}

The class has two accessor methods on the global List variable. You can call the ListTest.Record method to add a string value to the list, and then call Display to write the values to the screen.

Example 2. Next, we add three strings to the static List instance in the class above. These strings can now be accessed from the static List instance above, or displayed using the special Display method on the ListTest type.

C# program that uses class with List

using System;

class Program
{
    static void Main()
    {
	//
	// Use the static List anywhere.
	//
	ListTest.Record("Dot");
	ListTest.Record("Perls");
	ListTest.Record("Net");
	ListTest.Display();
    }
}

Output

Dot
Perls
Net

Using static instances. There are many ways you can use static instances of collections such as List. There is also the option of using singleton instances, which is a design pattern surrounding a static instance.

Singleton, Static Class

Summary. We used a static List instance in a C# program. The term static refers to the single-instance state of the variable, meaning it belongs to the enclosing type and not the object instance.

Note: For data collections that are not dynamic and do not expand, you can use a static array instead of a List.

Static Array


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf