Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
A Brief Introduction to Arrays
3.
Introduction to Programming in C#
4.
Working of Arrays in C#
5.
Implementing Arrays in C#
6.
Types of Arrays in C# with Examples
6.1.
One-dimensional Arrays
6.2.
Multi-dimensional Arrays   
6.3.
Jagged Arrays
7.
Frequently Asked Questions
7.1.
What are arrays?
7.2.
What is C#?
7.3.
Where is C# used?
7.4.
What are the different types of arrays?
7.5.
What are Jagged arrays?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Introduction to Arrays in C#

Author RAGHAV ANUSHA
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Hello Ninjas, Welcome back! If you are into coding and have learned different coding languages, you must know that arrays are standard in almost all programming languages. Arrays are data structures that store elements in a contiguous block of memory, where the data type of all elements is the same.

Arrays in C#


In this article, we will talk about arrays in C#, programming in C#, working of arrays in C#, implementation of arrays in C#, and types of arrays in C# with examples. So let's get started with understanding what arrays are. 

A Brief Introduction to Arrays

Arrays are fundamental data structures used in programming that store elements in a contiguous block of memory, where the data type of all elements is the same. These elements can be accessed by their respective index numbers. Arrays store large amounts of data, such as lists of numbers, tables, mathematical operations, or even images and videos. In these scenarios, arrays are used to store data in specific formats. Such as for storing images, we use a 2D array, where each element represents a pixel. 

What are Arrays?


Arrays are declared with a fixed size and a specific data type; for example - an array of integers named "num" can be declared as "int num [5];" here, we have declared an array called "num" that can hold five integer values, the size of the array can not change but the values stored inside it can be altered.  

As mentioned above, elements in an array are accessed using their respective index numbers. These index numbers represent the position of an element in the array. The first position or index assigned to an element is always zero, the second element has an index of one, and so on. Therefore, to access the second element of the "num" array, we will use "num[1]," and to access the third element, we will use "num[2]".

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Introduction to Programming in C#

C#, also known as C-Sharp, is an object-oriented programming language developed by Microsoft as part of its .NET initiative in 2000. C# was designed to be user-friendly and is used to build web, mobile, and gaming applications. C# allows the developers to organize their code into reusables objects and classes. This feature makes it easier to write reusable codes that can also be extended. C# is a versatile and powerful language, supporting other modern programming concepts such as delegates, generic, lambda expressions, etc. 

Programming in C#


C# is integrated with the .NET framework, which enables users to explore a wide range of libraries and tools, such as web services and working with databases. The syntax of C# is very similar to that of C/C++ and Java. C# supports similar control structures like loops, if-else statements, exception handling to handle errors, etc.

To get started with programming in C#, We need to set up a development environment like Visual Studio, which is ideal for creating new projects and writing codes. It is also a popular development environment for .NET development.   

Working of Arrays in C#

Arrays in C# are stored elements of the same data type. C# offers built-in support for many common operations, such as searching and sorting. In C#, arrays are declared by specifying the type of elements they will hold, followed by the array name and the size of the array. 

C# offers various built-in methods for using arrays, such as the "Sort()" method, the "Length" property, and the "IndexOf()" method. The "Sort()" method sorts the elements of any given array in ascending order. The "Length" property gives us the number of elements in the array. The "IndexOf()" method helps us to find the index of any specified element in an array. In C#, We can also create arrays of objects. In addition to these features, C# also supports multidimensional arrays, which can be understood as arrays of arrays in simple language. 

Implementing Arrays in C#

The first step to implementing an array is to declare and initialize a variable with a set of values. We can declare and initialize an array in the following ways -  

  • Declaring an array -
/*Declaring an array*/
data_Type[] name_array = new data_Type[size];
 


In the below example, we have declared and initialized the array, wherein [] stores the size of the array. For example -
 

/*Declaring an array of integers named "num," which can hold five integer values*/
int[] num = new int[5];
 


In the above example, we declared and initialized an array. Here, “int” is the data type, “num” is the name of the array, and “5” is the size of the array.
 

  • Declaring an array and assigning values  -
     
/*Declaring an array and assigning values*/
data_Type[] name_array = new data_Type[size]{values};
 


We have declared an array and assigned values in the below example. 
 

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


In the above example, we simultaneously declared and assigned the values in an array. Here, “int” is the data type, “num” is the name of the array, “5” is the size of the array, and “1,2,3,4,5” are values in the array.
 

  • Declaring an array and assigning values without mentioning the size of the array -
     
/*Declaring an array and assigning values without mentioning the size of the array*/
data_Type[] name_array = {values};
 


Here, the array size will be the total number of values taken by the user. For example -
 

int[] num = {1,2,3,4,5};
  • We can access the elements of any given array using their respective index numbers in the following manner -   
     
/*Setting the first element of the array to 5*/
num[0] = 5;
 
  • We use a "for loop" to iterate and perform actions on all the elements in an array. Let's take a look at this example to understand better - 
     
/*Using a for loop in C# programming language*/
for(int i=0; i<num.Length; i++)
 {
       Console.Writeline(num[i]);
 }


We are initializing a "for loop" for all the array elements in the above example. The array's name is "num," and we are using "num.Length" to cover all the elements of the loop.

Types of Arrays in C# with Examples

There are several arrays in C#, each with its properties and a different data access method. Let us explore these different types of arrays -  

One-dimensional Arrays

These types of arrays are the most commonly used arrays. They consist of a single row or column. Let's first look at the syntax  -  
 

/*Creating a one-dimensional array*/
data_Type[] name_array = new data_Type[size];
 


For example - 
 

/* Creating a One-dimensional Array named “num” of size equal to 5. */
int[] num = new int[5];
 


In the above example, we have created a one-dimensional array. The data type here is an integer, “num” is the name of the array, and “5” is the size of the array.  

Multi-dimensional Arrays   

These arrays store matrices with two or more rows and columns. We can create a two-dimensional array to store a matrix. Let's first look at the syntax  -  
 

/*Creating a two-dimensional array*/
data_Type[,] name_array = new data_Type[ rows, columns];


Let us create a 2X2 matrix of integers - 

 

/*Creating a 2X2 matrix*/
int[,] matrix = new int[2,2];

 

We must use the correct indices for every dimension to access elements in a multidimensional array. For example, to access the element in the second row and first column of the matrix, we will write the - 
 

/*Accessing the element in the first row and the first column of the matrix*/
int element = matrix[1,0];

Jagged Arrays

These arrays are used to create complex data structures and come in handy in situations where the size of the sub-arrays is different. Let's look at the syntax for creating a jagged array -  

 

/*Creating a Jagged array*/
data_Type[][] name_array = new data_Type[size][];

 

Let us create a jagged array of four arrays of integers - 

 

/*Creating a jagged array of four arrays of integers*/
int[][] jaggedArray = new int[4][];
 


In conclusion, as discussed above, there are various arrays, such as one-dimensional, two-dimensional, and jagged arrays. Each array has separate properties and a different method to access the data within them. Let's explore more about the different types of arrays in C#.  

Check out this problem - First And Last Occurrences Of X.

Must Read IEnumerable vs IQueryable.

Frequently Asked Questions

What are arrays?

Arrays are fundamental data structures used in programming that store elements in a contiguous block of memory, where the data type of all elements is the same.    

What is C#?

C#, also known as C-Sharp, is an object-oriented programming language developed by Microsoft as part of its .NET initiative in 2000.  

Where is C# used?

C# was designed to be user-friendly and is used to develop web, mobile, and gaming applications. 

What are the different types of arrays?

In C#, Arrays are of numerous types, such as one-dimensional, two-dimensional, or jagged arrays. Each array has different properties and a different method to access the data.   

What are Jagged arrays?

Jagged arrays in simple language are also called "arrays of arrays." Each of the main array's elements in jagged arrays is an array. 

Conclusion

This article discussed arrays in C#, programming in C#, working of arrays in C#, implementation of arrays in C#, and types of arrays in C# with examples. If you want to dig deeper into arrays, here are some related articles - 
 

Recommended problems -

 

You may refer to our Guided Path on Code Studios to enhance your skill set on DSA and many more. Check out essential interview questions, practice our available mock tests, and so much more!

Live masterclass