Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

C# arrays with examples


In another programming like C, C++ and PHP etc, arrays are similar.
An array is a special variable that can contain a lot of similar data item. In this tutorial, we are going to learn arrays and types of arrays in C# programming.


Arrays in C #


The array is a collection of similar data items. C# supports various types of arrays. In C# programming, an array is an object of base type System. Array. If you are already learned array in another programming then it will be easy to understand. The array index starts from the 0. like 0, 1,2,3,4, etc. C# supports to store the fixed size of elements in the array. The C# array is known as a data structure to stored data.

Why use array in C# programming?

This question comes out in every beginner. In C# programming, the array helps to remove complexity. Array optimize the code. It reduces the length of code. We can access array data randomly. If you want to traverse data, it makes easy. The manipulation is easy with the array.

There are a lot of reasons to use Arrays in C# programming.

 

How to declare and initialize the Array in C#?


Unlike other programming languages, the array is similar to declare. We can initialize an array at the time of declaration.

Let's discuss C# array declaration with example.

int[] record=new int[7] {5,15,20,25,30,35,40}; 
In other way , we cane blank size of array like this .
int[] record=new int[] {5,15,20,25,30,35,40};
In simple way , we do not need to use new operator . We can condone new operator like this .
int[] record=[] {5,15,20,25,30,35,40};

We declared, initialize and assigned values to C# array.

 

How to access the array element in C#?


The process of accessing data from the array is very easy. We can access data using the array name and the index number. Arrays index number starts from the zero.
Access the array elements.

record[0]
record[1]
record[2]
record[3]
record[4]
record[5]

You can specify the number of the index to get the array element.

Let's create an example of a C# array.

 

Example of C# array.


using System;

namespace DowhileLoop{

   public class Program {
   
      public static void Main(string[] args) {

   int[] data=new int[6] {67,89,23,21,56,32}; 
		  
	Console.WriteLine(data[0]);
	Console.WriteLine(data[1]);
	Console.WriteLine(data[2]);
	Console.WriteLine(data[3]);
	Console.WriteLine(data[4]);
	Console.WriteLine(data[5]);
	}
}	 
}

Output -

67
89
23
21
56
32
 

In the above example, we declared and initialized the array with values. We printed array values by C# array name and the index number. This is a simple example of a C# array.

 

Create an example of for loop with C# array| Traversing array.


A programmer never tired to try new code and think innovative. Let's create an example of the array using for loop. We had already discussed C# for loop . In this example, we will traverse the array.


using System;
namespace ArrayApplication{

   public class Program {
   
      public static void Main(string[] args) {

   int[] record={5,10,25,20,25,30}; /*decaring and initializing array */
		  /*traversing array  */
		 for(int a=0; record.Length>a; a++)
		 {
Console.WriteLine(record[a]); 
		 }
		  

	}
}	 
}


Output - 

 
5
10
25
20
25
3

In the above C# example, we declared and initialized the array at a time. We omitted the new operator in this example and used for loop to traverse an array. The most benefit of traversing array, we do not need to specify index number one by one.

Let's create another example of C# array with foreach loop.

 

Traversing array with foreach loop in C# programming.


In the above example, we traversed the array using for loop. In the example, we are going to traverse array using forearch loop in C#.
Let's have a look.


using System;
namespace ArrayApplication{

   public class Program {
   
      public static void Main(string[] args) {

   int[] record={5,10,25,20,25,30};   /*decaring and initialisig array */
/*traversing array using foreach loop */
		  
		foreach(int a in record)
              {
			Console.WriteLine(a); 
		 }
		  

	}
}	 
}

Output- 

 
5
10
25
20
25
30

Types of the array in C# -

There are three types of arrays in C# language.


1. Single dimensional array


We have discussed above the single-dimensional array. The single-dimensional array is known as a liner array. In single dimensional array, we used bracket after type.

2.Multidimensional arrays


The multidimensional array has rows and columns. C# support multidimensional array.

Learn C# Multidimensional arrays


3.Jagged array

A jugged is an array of array. Learn complete jugged array in C# programming.

Learn C# Jugged array
Please Share

Recommended Posts:-