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.

Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

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

Popular Posts