Home
C#
static Array
Updated Sep 12, 2023
Dot Net Perls
Static array. An array can be static—this means it is shared among all instances of classes in a program. Each class reuses the same array.
Array
static
Notes, static arrays. This pattern is commonly used in C# programs and often useful. Static arrays can reduce allocations, which can improve performance.
First example. To start, we use a static integer array. The data shown is separate from the state of the object. The list of prime numbers is not affected by state.
int Array
Tip Your program can store static or constant data like these prime numbers for efficient access and use.
Detail This class contains a static int array that remains present throughout the lifetime of the program.
Detail The _primes array has an underscore at its start. This is a common pattern and helps show it is a field.
Note This array is initialized to 5 integers, and can be used without worrying about a class instance.
using System; using System.Linq; class Program { static void Main() { // Use the public method to test static array. bool isWagstaff = NumberTest.IsWagstaffPrime(43); Console.WriteLine(isWagstaff); // Test static array again. isWagstaff = NumberTest.IsWagstaffPrime(606); Console.WriteLine(isWagstaff); } } /// <summary> /// Contains static int array example. /// </summary> public static class NumberTest { /// <summary> /// This static array contains several Wagstaff primes. /// </summary> static int[] _primes = { 3, 11, 43, 683, 2731 }; /// <summary> /// Public method to test private static array. /// </summary> public static bool IsWagstaffPrime(int i) { return _primes.Contains(i); } }
True False
Example 2. Here we use a static string array to store type-specific information such as an array of dog breeds. This data is constant. It does not need to be frequently changed.
Detail We use the StringTest.Dogs property. The StringTest class contains an array of dog breed strings.
Property
using System; class Program { static void Main() { foreach (string dog in StringTest.Dogs) { Console.WriteLine(dog); } } } /// <summary> /// Contains static string array. /// </summary> public static class StringTest { /// <summary> /// Array of dog breeds. /// </summary> static string[] _dogs = new string[] { "schnauzer", "shih tzu", "shar pei", "russian spaniel" }; /// <summary> /// Get array of dog breeds publicly. /// </summary> public static string[] Dogs { get { return _dogs; } } }
schnauzer shih tzu shar pei russian spaniel
List. A static List can store a variable number of elements in one location. The code can be used for much larger collections, and collections of varying sizes.
static List
List
ArrayPool. An ArrayPool can make the reuse of arrays more powerful—we can specify minimum lengths required. And we can "rent" arrays to reduce conflicts.
ArrayPool
A summary. We used static data structures such as int arrays and string arrays. These static collections are useful to access data that doesn't depend on an instance.
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.
This page was last updated on Sep 12, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen