C# Arrays

Single-Dimension Array

An array is a set of data items of the same type. Each data item can be accessed using a numerical index. You declare an array as follows:

// Create an array named aBunchOfInts containing 5 elements indexed 0,1,2,3,4
int[] aBunchOfInts = new int[5];

Once defined, you can fill it using the index. (always remember that arrays start with an index of 0).

aBunchOfInts[0] = 5;
aBunchOfInts[1] = 10;
aBunchOfInts[2] = 15;
aBunchOfInts[3] = 20;
aBunchOfInts[4] = 25;

Be aware that if you declare an array but don’t explicitly fill each index, each item will be set to the default value of the data type. (in this example, because its an array containing ints, each int in the array would be a default of 0).

An array can be initialized at the same time it’s being declared like so:

int[] aBunchOfInts = new int[] {5,10,15,20,25};

The var keyword can be used to define an implicitly typed array. this way you can allocate a new array variable without specifying the type contained within the array itself.

// a is an array of ints
var a = new[] {5, 10, 15, 20, 25};

// b is an array of doubles
var b = new[] {2.2, 3.3, 4.4, 5.5, 6.6};

// c is an array of strings
var c  new[] {"hi", "how", "are", "you?"};

Multidimensional Arrays

Rectangular Multidimensional Array

There are two types of multidimensional arrays. The first is a rectangular array, which is an array of multiple dimensions where each row is the same length. The following code initializes a rectangular multidimensional array with 3 rows and 4 columns and then uses a for loop and a nested for loop to fill it with values.

// a rectangular multidimensional array
int[,] myRectangularMDArray;
myRectangularMDArray = new iny[3,4];

// Fill the (3 by 4) array
for(int i = 0; i < 3; i++)
     for(int j = 0; j < 4; j++)
          myRectangularMDArray[i, j] = i * j;

// Print the (3 by 4) array
for(int i = 0; i < 3; i++)
     for(int j = 0; j < 4; j++)
          Console.Write(myRectangularMDArray[i, j] + "\t");

this example would print out the following to the console window:

0     0     0     0

0     1     2     3

0     2     4     6

Jagged Multidimensional Array

A jagged array contains some number of inner arrays, each of which may have a different upper index limit. The following code initializes a jagged multidimensional array and then fills it with varying length arrays.

// A jagged multidimensional array (i.e., an array of arrays).
// Here we have an array of 5 different arrays.
int[][] myJagArray = new int[5][];

// Create the jagged array.
for (int i = 0; i < myJagArray.Length; i++)
     myJagArray[i] = new int[i + 7];

// Print each row.
// Because we didn't fill each element with a specific int value, 
// each element will have its default int value which is zero(0)
for (int i = 0; i < 5; i++)
     for (int j = 0; j < myJagArray[i].Length; j++)
          Console.Write(myJagArray[i][j] + " ");

this example would print out the following to the console window:






Multidimensional arrays take a little getting used to, the nested for loops will take a bit of effort to visualize in your head but once you wrap your head around them they become pretty straight forward.

New in C# 7 – Digit Separators and Binary Literals

C# 7 has added digit separators which makes it easier for the eye to keep track of the really long numbers in code. The underscore ( _ ) can now be used as a separator for integers, long, float, double and decimal data types.

static void ShowDigitSeparators()
     Console.WriteLine("***** Digit Separators *****");

digit separator output

C# 7 has also added Binary Literals which makes it easier to see your bit masks, and guess what?… the new digit separator works with these also!

static void ShowBinaryLiterals()
     Console.WriteLine("***** Binary Literals With Digit Separators: *****");
     Console.WriteLine("Sixteen: {0}",0b0001_0000);
     Console.WriteLine("Thirty Two: {0}",0b0010_0000);
     Console.WriteLine("Sixty Four: {0}",0b0100_0000);

binary literals

These are two small features that make life as a C# programmer just that much easier.

Format Numerical Data

C# offers some handy numerical format characters.

  • C or c – used to format currency.
  • D or d – used to format decimal numbers. It can also specify the minimum number of digits used to pad the value.
  • E or e – used for exponential notation. if you use upper case the exponential constant will be uppercase, if you use lowercase it will be lowercase.
  • F or f – used for fixed point formatting. It can also specify the minimum number of digits used to pad the value.
  • G or g – stands for general. Can be used to format a number to fixed or exponential format.
  • N or n – used for basic formatting with commas.
  • X or x – used for hexadecimal formatting. If you use uppercase the hex values will be in uppercase and vice versa.

here are some examples.

class Program
    static void Main(string[] args)
        Console.WriteLine("***** Formatting Numerical Data *****");
        Console.WriteLine("The value 99999 in various formats:");
        Console.WriteLine("c format: {0:c}", 99999);
        Console.WriteLine("d9 format: {0:d9}", 99999);
        Console.WriteLine("f3 format: {0:f3}", 99999);
        Console.WriteLine("n format: {0:n}", 99999);
        Console.WriteLine("E format: {0:E}", 99999);
        Console.WriteLine("e format: {0:e}", 99999);
        Console.WriteLine("X format: {0:X}", 99999);
        Console.WriteLine("x format: {0:x}", 99999);


the previous code produces the following output.