Monday, 18 April 2016

Programming Arrays in C#
Programming C# is a new self-taught series of articles, in which I demonstrate various topics of C# language in a simple step by step tutorial format.
Arrays are probably one of the most wanted topics in C#. The focus of this article is arrays in C#. The article starts with basic definitions of different array types and how to use them in our application. Later, the article covers the Arrays class and its methods, which can be used to sort, search, get, and set an array's items.
Introduction
In C#, an array index starts at zero. That means the first item of an array starts at the 0th position. The position of the last item on an array will total number of items - 1. So if an array has 10 items, the last 10th item is at 9th position.
In C#, arrays can be declared as fixed length or dynamic.
A fixed length array can store a predefined number of items.
A dynamic array does not have a predefined size. The size of a dynamic array increases as you add new items to the array. You can declare an array of fixed length or dynamic. You can even change a dynamic array to static after it is defined.
Let's take a look at simple declarations of arrays in C#. The following code snippet defines the simplest dynamic array of integer types that does not have a fixed size.
int[] intArray;

As you can see from the above code snippet, the declaration of an array starts with a type of array followed by a square bracket ([]) and name of the array.
The following code snippet declares an array that can store 5 items only starting from index 0 to 4.
int[] intArray;
intArray = new int[5];

The following code snippet declares an array that can store 100 items starting from index 0 to 99.
int[] intArray;
intArray = new int[100];

No comments:

Post a Comment