Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
482 views
in Blog Post by 15 41 68
edited by

In this post, we're gonna learn about arrays in C#.

What is an Array?

Array is a data structure that used to store multiple values in one single variable instead of declaring separate variables for each value.

The major operations in data structure are (Access, Insert, Delete, Find, Sort) an element  

Arrays in C#

  • An array is an object of base type system.array
  • In C#, all arrays are dynamically allocated.
  • An array is a data structure that holds data (values) of the same data type.
  • An array that is declared like the other variables with [] after the data type.
  • Variables in an array are ordered and each variable has an index starting from 0 that the element can be accessed by it.
  • Arrays are objects in C# so we can find its length using member length.
  • Array element can be of any type like int, string, ....including an array type.

Array declaration in C#

datatype[] arrayName;

Data Type: It defines the element type of the array.
[ ]: It defines the size of the array.
arrayName: It is the Name of array

We declare arrays to be of a certain data type

Ex:

int[] ages;
String[] names;
float[] weights

declaration of an array in3 c


Access an array element in C#

  • Each element in the array has an index that we can access the element by it.
  • The index starts with zero and goes up one at time 

For example when you want to return the first element in the array:-

return element of an array in c#


Array length in C#

  • Since arrays are objects in C#, we can find its length using the length property.
  • The array contains specific numbers of element.
Console.WriteLine(cars.Length);

// Outputs 4

Looping array elements in C#

by using the for loop and the length property of the array we can looping the array elements.

looping array in C#


Sort arrays in C#

  • Sort() is an array method that used to sort the array element alphabetically in ascending order.
  • We can sort the array in descending order after sort(), we will use the reverse() method.

sort array in C#


C# array slices

  • Array slices is an operation that extracts a subset of elements from an array and packages them as another array.
  • We can use the .. operator to get array slices.
  •  The start of the range is included, but the end is excluded

We will use the cars array :

string [ ] cars={"volvo", "BMW", "Ford", "Mazda"};

Ex1:

Create an array slice containing elements from index 1 to index 3.

Element at index 1 is included but element at index 3 is excluded.

string[] cars1 = cars[1..3];
Console.WriteLine("[{0}]", string.Join(", ", cars1));
//the output will be [BMW, Ford]

Ex2:

Create an array slice containing elements from the first index 0 to index 3.

Note : if the start index is omitted then it will begin from range 0.

string[] cars2 = cars[..3];
Console.WriteLine("[{0}]", string.Join(", ", cars2));
//the output will be [Volvo, BMW, Ford]

Ex3:

Create an array slice containing elements from the first index 1 to the end.

Note : if the end index is omitted then it will end at last index.

string[] cars3 = cars[1..];
Console.WriteLine("[{0}]", string.Join(", ", cars3));
//the output will be [BMW, Ford, Mazda]

Conclusion 

In this post, we have taken a tour of arrays in C# and we explored some of its important methods.

See Also


If you don’t ask, the answer is always NO!
...