A One Stop Solution to Understand C# Arrays

Throughout the years, C# has added features to support new workloads and new emerging software design practices. It is an object-oriented, component-oriented language, C#. C# provides language constructs that directly support these ideas to create and use software components.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

What Are C# Arrays?

An array in C# is a collection of elements of the same type stored in the exact memory location. For example, in C#, an array is an object of base type "System.Array". The array index in C# starts at 0. In a C# array, you can only store a fixed number of elements.

Now you will explore the different types of C# Arrays.

Types of C# Arrays

C#_Arrays-types-img1

There are Three types of Arrays in C#:

  1. Single Dimensional Arrays
  2. Multidimensional Arrays
  3. Jagged Arrays

Now, discuss them in detail, and get started with Single dimensional Arrays.

What Is a Single Dimensional C# Arrays?

C#_Arrays-one-d-array-img1

Arrays with only one row for storing the values. This array's values are stored in a contiguous manner, beginning with 0 and ending with the array size. Therefore, you must use square brackets [] to make a single-dimensional array after the data type.

Now, create a Single-dimensional array using code.

Code:

//A C# Program to implement a one-dimensional array

using System;

public class OnedArray

{

    public static void Main(string[] args)

    {

        int[] array= {10, 15,13,9,21};//Array definition

//A for loop to print the Array elements

        for(int j=0;j<array.Length;j++)

        {

            Console.WriteLine(array[j]);

        }

    }

}

C#_Arrays-one-d-array-img2.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

What Is Multidimensional C# Arrays?

C#_Arrays-two-d-array-img1

The multidimensional array has multiple rows for storing values. It is also known as a Rectangular C# Array. It can be a 2D, 3D, or more array. A nested loop was required to store and access the array's values. Therefore, you must use commas inside the square brackets to create a multidimensional array.

Now, create a two-dimensional array using code.

Code:

//A C# program to implement 2-D Array in C#

using System;

public class MultidArray

{

    public static void Main(string[] args)

    {

        int[,] array=new int[,]{{1,2,3},{4,5,6},{7,8,9}};

        for(int i=0;i<3;i++){

            for(int j=0;j<3;j++){

                Console.WriteLine(array[i,j]+" ");

            }

            Console.WriteLine();

        }

    }

}

C#_Arrays-two-d-array-img2

What Are Jagged C# Arrays?

C#_Arrays-jagged-array-img1.

A jagged array in C# is called an "array of arrays" in C# since each element is an array. A jagged array's element size might vary.

Now, go ahead and create a Jagged array using code.

Code:

//A C# program to implement jagged Arrays

using System;

public class JaggedArray

{

    public static void Main(string[] args)

    {

        int[][] array=new int[2][]; //definition of array

        array[0]=new int[6]{42,61,37,41,59,63};

        array[1]=new int[4]{11,21,56,78};

        for(int i=0;i<array.Length;i++)

        {

            for(int j=0;j<array[i].Length;j++)

            {

                System.Console.Write(arr[i][j]+" ");

            }

            System.Console.WriteLine();

        }

    }

}

C#_Arrays-jagged-array-img2

And these are the types of C# Arrays. Now have a look at the advantages of C# Arrays.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Advantages of C# Arrays

  • It is used to represent numerous data objects of a similar kind with a single name.
  • Data structures, like linked lists, stacks, trees, and queues, are implemented using arrays.
  • The two-dimensional arrays in C# are used to represent matrices.
  • Since arrays are strongly typed, the program's performance is improved because of no boxing and unboxing.

Disadvantages of C# Arrays

  • The array size is fixed. So, you must declare the size of the array beforehand.
  • If you allocate more memory than the requirement, the extra memory will be wasted.
  • An element can never be inserted in the middle of an array.
  • It is also impossible to delete or remove elements from the middle of an array.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Next Steps

"C# Interfaces" can be your next concept to learn. For example, in C#, the Interface is like a class blueprint. However, as with abstract classes, any methods declared within an interface are abstract methods. For example, it doesn't have a method body.

If you want to learn more about software development and possibly work in it, you've come to the right place. Our Post Graduate Program In Full Stack Web Development are taught in collaboration with Caltech CTME. These classes cover data structures and algorithms from fundamentals to advanced concepts like Competitive Programming. As a software developer, you will learn about data structures like trees, graphs, and queues. 

If you have any questions about this "C# Arrays" tutorial, please drop them in the comment section below. Our 24/7 expert team will promptly respond to your queries.

About the Author

Kartik MenonKartik Menon

Kartik is an experienced content strategist and an accomplished technology marketing specialist passionate about designing engaging user experiences with integrated marketing and communication solutions.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.