×

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# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 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# 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.

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# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes 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# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 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.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 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.

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.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 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.

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# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.