The main purpose of "Dot Net Labs" provide the dot net program. You can learn dot net, .Net Core, C#, SQL, Linq step-by-step.

Monday 22 May 2023

Introduction to Arrays in C#


 Array

In C#, an array is a data structure that stores a fixed-size sequence of elements of the same type. Arrays are widely used in programming for tasks that involve working with collections of data, such as storing a list of numbers, strings, or objects.

Example :      int[] numbers = new int[5];

Describe:

  • An array always stores values of a single data type

          Example:

          int[] numbers = {2,4,5,7};   //allow single data type


          int[] numbers = {2,4,5,"Amit" };  //compile error, convert type string to int

  • C# supports zero-based index values in an array
          Example:
          int[] numbers = { 1, 2, 3, 4, 5 };
          
  • Arrays have a fixed length, means that once your create an array with a specific size, you cannot change its size later, The size of an array is determined at the time of its declaration.
            int[] myarray = new int[4];

            myarray[0] = 1;
            myarray[1] = 2;
            myarray[2] = 3;
            myarray[3] = 4;

Declaring Arrays
Arrays are reference type variable whose creation involves two step:
  1. Array declaration specifies the type of data
  2. Array name, basically it is an identifier
  3. Declaring an array does not allocate memory to the array
      Declare an array with memory allocation

        int[] myarray;
what is array in c#
Initializing Arrays
We can initialize many way see below example

Initialized at the time of declaration iteself
Example : int[] myarray = { 1, 2, 3, 5, 4, 8, 6 };

Use index number to initialize of array.

myarray[0] = 78;

myarray[2] = 4233;

myarray[3] = 3;

myarray[4] = 96;

Access Array Elements

You can use the index of the desired element within square brackets ([]) after the array variable.

An array index always starts at 0.

Example:

            int[] numbers = { 1, 2, 3, 4, 5 };

            // Accessing the first element

            int firstNumber = numbers[0];  // Value: 1

            // Accessing the third element

            int thirdNumber = numbers[2];  // Value: 3


You can retrieve the value at that particular position in the array or modify it.

Accessing array using for loop
            
            int[] numbers = new int[5];
            numbers[0] = 1;
            numbers[1] = 2;
            numbers[2] = 3;
            numbers[3] = 4;
            numbers[4] = 5;
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.WriteLine(numbers[i]);
            }

Accessing array using for foreach loop
Example 1:
                      int[] numbers = new int[5];
            numbers[0] = 1;
            numbers[1] = 2;
            numbers[2] = 3;
            numbers[3] = 4;
            numbers[4] = 5;
 
            foreach (int item in numbers)
            {
                Console.WriteLine(item);
            }

Example 2:
                   string[] animals = new string[5];
            animals[0] = "Elephant";
            animals[1] = "Tiger";
            animals[2] = "Lion";
            animals[3] = "Bear";
            animals[4] = "Hourse";
 
            foreach (string item in animals)
            {
                Console.WriteLine(item);
            }

Modify Array ElementsYou can modify the elements of an array. You simply assing a new value to that desired index.
            
            // create an array
            int[] numbers = { 1, 2, 3 };
 
            Console.WriteLine("Old Value at index 0: " + numbers[0]);
 
            // change the value at index 0
            numbers[0] = 11;
 
            //print new value
            Console.WriteLine("New Value at index 0: " + numbers[0]);

            Output
            Old Value at index 0: 1
            New Value at index 0: 11


0 comments:

Post a Comment

Do not enter spam link

Popular Posts