ankitweblogic logo
W e b t u t o r i a l s

Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators


Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

Exercise Loops & Nested Loop

C# Arrays

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection


C#.Net Arrays

An array is a collection of similar values. An array contains a number of variables, which are accessed through indexes. All elements of an array have to be same data type.
Types of Arrays:
1. Single Dimensional
Contain only a single row and multiple columns. Syntax for declaring 1D Array is:
int [ ] arrayname = new int [10]; OR
int [ ] arrayname = { 5, 10, 20, 15, 3 };

Array Operations
a. Copy Array
int[] arr = { 1, 2, 3 };
int[] arr2 = (int [])arr.Clone();
Array.Copy (arr, arr2, 5);

b. Sorting: work on numbers as well as on strings.

c. Reverse:

2. Two-Dimensional
The two-dimensional arrays are declared, allocated and initialized like one-dimensional arrays. However you have to specify two dimensions rather than one.
For example the following table represents the 3 x 5 dimensional arrays. It has a total of 15 elements in it.

            C0	C1  C3	C4  C5
        R0  1	2   3	4   5
        R1  6	7   8	9   10
        R2  11	12  13	14  15

Syntax for declaring a two-dimensional array is:
int [ , ] arr = new int [ 3, 5 ];

3. Jagged Arrays ( Arrays of Arrays )
Multidimensional arrays are said to be rectangular i.e. each row is of same size. It is possible to have jagged arrays, where rows may be of different size. The syntax is:

    int [ ] [ ] jagged = new int [2][ ];
    jagged[0] = new int[3];
    jagged[1] = new int[4];
    foreach (int[] a in jagged)
        foreach (int x in a)
    4. Array Class: 
        Array intArray = Array.CreateInstance(typeof(int), 5);
        intArray.SetValue(10, 4);
        intArray.SetValue(20, 3);
        intArray.SetValue(30, 2);
        intArray.SetValue(40, 1);
        intArray.SetValue(50, 0);
        foreach (int a in intArray)
Updated: 03-Feb-19