×

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# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes 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# 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# 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# 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# 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# 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# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute 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# 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# 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# 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# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

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

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# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

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

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.