C# Static Array

These C# example programs use static array fields. Static arrays store data in one place.
Static array. An array can be static. It can store values that are not specific to instances of classes in the C# language. This pattern is commonly used in C# programs and often useful. We examine some example code.
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. The array contains the first five Wagstaff prime numbers.Int Array

Tip: Your program can store static or constant data like these prime numbers for efficient access and use.

NumberTest: This class contains a static int array that remains present throughout the lifetime of the program.

Underscore: 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 five integers, and can be used without worrying about a class instance.

C# program that uses static int array using System; using System.Linq; class Program { static void Main() { // Use the public function 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); } }
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 and doesn't need to be changed frequently, so it is inefficient to create it in multiple instances.Array

Property: The first part of the code shows how the StringTest.Dogs property is used. The StringTest class contains an array of dog breed strings.

PropertyString Property
C# program that uses static array 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; } } }
List. This site also contains an example of a static List that can store a variable number of elements in one location. That code can be used for much larger collections and collections of varying sizes, not just fixed sizes as with arrays.Static ListList
Summary. Here we saw examples of using static data structures such as integer arrays, string arrays, and Lists. These static collections are useful to access data that doesn't depend on an instance.Static
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls