Saturday, October 20, 2018

C# Array Tutorial: Create, Declare, Initialize

What is an Arrays in C#?

An array is used to store a collection or series of elements. These elements will be of the same type.
So for example, if you had an array of Integer values, the array could be a collection of values such as [1, 2, 3, 4]. Here the number of elements in the array is 4.
Arrays are useful when you want to store a collection of values of the same type. So instead of declaring a variable for every element, you can just declare one variable.
This variable will point to an array or list of elements, which will be responsible for storing the elements of the array.
Let's look at how we can work with arrays in C#. In our example, we will declare an array of Integers and work with them accordingly.
Note that all of the below code is being made to the Program.cs file.
Step 1) Declaring an array – The first step is to declare an array. Let's see how we can achieve this by the below code example.
Code Explanation:-
  1. The first part is the datatype. It specifies the type of elements used in the array. So in our case, we are creating an array of Integers.
  2. The second part [ ], which specifies the rank of the array. (The rank is a placeholder which specifies the number of elements the array will contain)
  3. Next is the Name of the array which in our case is 'values'. Note you see a green squiggly underline, don't worry about that. That is just .Net saying that you have declared an array, but not using it anywhere.
Step 2) The next step is to initialize the array. Here we are going to specify the number of values the array will hold. We are also going to assign values to each element of the array.
Code Explanation:-
  1. First, we are setting the number of elements the array will hold to 3. So in the square brackets, we are saying that the array will hold 3 elements.
  2. Then we are assigning values to each element of the array. We can do this by specifying the variable name + the index position in the array.
    So values[0] means that we are storing a value in the first position of the array. Similarly to access the second position, we use the notation of values[1] and so on and so forth.
    Note: - In Arrays, the index position starts from 0.
Step 3) Let's now display the individual elements of the array in the Console. Let's add the below code to achieve this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
class Program
namespace DemoApplication { {
Int32[] value;
static void Main(string[] args) { value=new Int32[3];
value[0]=1; value[1]=2; value[2]=3; Console.WriteLine(value[1]);
Console.WriteLine(value[2]); Console.ReadKey(); }
Code Explanation:-
This is the simple part wherein we just use the Console.Write method to send each value of the element to the console.
Note that again, we are accessing each element with the help of the array variable name along with the index position.
If the above code is entered properly and the program is executed, the following output will be displayed.
From the output, you can see all the values of the array being displayed in the Console.

No comments:

Post a Comment