×

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0). It is an object of the base class System.Array .

Advantages of using arrays:
  1. Code reusability
  2. Random access
  3. Less code
  4. Easy traversing
  5. Easy manipulation
Disadvantages:
  1. Can't increase its size at runtime.
Collections are used in C# to overcome size related problems of array.

Types of array:
  1. Single dimensional array
  2. Multi dimensional array
  3. Jagged array
Single Dimensional Array Single dimensional array is the array which has a single dimension and doesn't require any row or columns on a high level. It is stored in contiguous memory location.
Declaring 1D Array : To declare 1D array, we have to use square brackets [] after the data type. Let's see the syntax:

Syntax
<data type>[] <variable-name> = new <data type>[<size>];
Example:
int[] arr=new int[5]
let's see a simple example which initialize and traverse 1D array
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)  
{  
int[] arr = new int[5];//creating array  
arr[0] = 10;//initializing array  
arr[2] = 20;  
arr[4] = 30;  
//traversing array  
for (int i = 0; i < arr.Length; i++)  
{  
Console.WriteLine(arr[i]);  
}  
}  
}
Output
10
0
20
0
30
Declaring and initializing 1D array at the same time: We can declare and initialize 1D array at the same time. Let's see how can we do it . int[] arr=new int[5]{10,20,30,40,50}; We are not required to use size int[] arr=new int[]{10,20,30,40,50}; We can omit new operator also Int[] arr={10,20,30,40,50}; Let's see a simple example of initializing 1D array.
using System;
public class ArrayExample1
{
            public static void Main(string[] args)
            {
                        int[] arr={10,20,30,40,50,60};
                        for(int i=0;i<arr.Length;i++)
                        {
                                    Console.WriteLine(arr[i]);
                        }
            }
}
Output
10
20
30
40
50
60
Traversing array using foreach loop
We can use foreach loop in C# to traverse array elements. Let's see how can we do it.
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)  
{  
int[] arr = { 10, 20, 30, 40, 50 };//creating and initializing array  
//traversing array  
foreach (int i in arr)  
{  
Console.WriteLine(i);  
}  
}  
}
Output
10
20
30
40
50

Related Topics

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.