Stats

785 Contributors: 26 Monday, July 17, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Arrays

Syntax

  • Declaring an array:

    <type>[] <name>;

  • Declaring two-dimensional array:

    <type>[,] <name> = new <type>[<value>, <value>];

  • Declaring a Jagged Array:

    <type>[] <name> = new <type>[<value>];

  • Declaring a subarray for a Jagged Array:

    <name>[<value>] = new <type>[<value>];

  • Initializing an array without values:

    <name> = new <type>[<length>];

  • Initializing an array with values:

    <name> = new <type>[] {<value>, <value>, <value>, ...};

  • Initializing a two-dimensional array with values:

    <name> = new <type>[,] { {<value>, <value>}, {<value>, <value>}, ...};

  • Accessing an element at index i:

    <name>[i]

  • Getting the array's length:

    <name>.Length

Remarks

In C#, an array is a reference type, which means it is nullable.

An array has a fixed length, which means you cant .Add() to it or .Remove() from it. In order to use these, you would need a dynamic array - List or ArrayList.

Related Examples