Sep 20, 2011

Array declaration in c sharp

Array is reference type data type in c# which keeps collection of similar types of data. In C# array is different from array in c or c++. In C# Array is abstract class and it is base class of all other types of arrays. It means we can use all the properties and methods of class Array in any other types of arrays like array of int, array of float. For example Length is property of class Array which returns total number of elements in the array and we can use this property in any type of array in C#. We will go through the properties and methods.  Arrays can be single dimensional or multi dimensional. In C# array can be declared in many ways:

Declaration of single dimensional array in C#

Process 1:
Declaring and defining array separately:

int[] arr;
arr = new int[10]; 

Sample code:
using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaring the array
            int[] arr;

            //defining the array
            arr = new int[3];

            //assining values to array
            arr[0] = 5;
            arr[1] = 10;
            arr[2] = 15;

            Console.WriteLine("Length of array: {0}", arr.Length);
           
            //printing each elements of array
            foreach (int element in arr)
            {
                Console.WriteLine(element);
            }
            Console.ReadKey();
        }
    }
}

Output:
Length of array: 3
5
10
15

Process 2:
Declaring and defining the array at the same time:

int[] = new int[10];

Sample code:
using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaring and defining the array
            int[] arr = new int[3];

//assining values to array
            arr[0] = 5;
            arr[1] = 10;
            arr[2] = 15;

            Console.WriteLine("Length of array: {0}", arr.Length);
           
            //printing each elements of array
            foreach (int element in arr)
            {
                Console.WriteLine(element);
            }
            Console.ReadKey();
        }
    }
}

Output:
Length of array: 3
5
10
15

Process 3:
Declaring, defining and assigning values at the same time:

int[] arr = new int[] { 5,10,15 };
or
int[] arr = { 5,10,15 };

Sample code:
using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaring, defining and assigning values to array at the same time
            float[] arr = new float[]{5.0f,10.0f,15.5f};
            //float[] arr = {5.0f,10.0f,15.0f};
           
   
            Console.WriteLine("Length of array: {0}", arr.Length);
           
            //printing each elements of array
            foreach (float element in arr)
            {
                Console.WriteLine(element);
            }
            Console.ReadKey();
        }
    }
}

Output:
Length of array: 3
5
10
15.5


Important points about arrays in c#


1. Default values of array in c#:

A default value of elements of array depends upon default value of type of array. For example if array is type int then its default values will 0 since default value of int is 0.

Default values of array of byte, short, int: 0
Default values of array of bool: false
Default values of array of float: 0.0f or 0.0F
Default values of array of double: 0.0d or 0.0D
Default values of array of decimal: 0.0m or 0.0M
Default values of array of long: 0.0l or 0.0L
Default value of array of any reference type is: null


Sample code:
using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            bool[] arr = new bool[3];
   
            //printing default values of each elements in the array
            foreach (bool element in arr)
            {
                Console.WriteLine(element);
            }
            Console.ReadKey();
        }
    }
}

Output:
False
False
False

2. Types of array in C#:
In C# array can be any type except array of void.

Array of bool in C#

Declaring and defining the array of boolean at the same time in C#

Sample code:
using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            bool[] arr = new bool[4];

            arr[0] = true;
            arr[1] = false;
            arr[2] = Convert.ToBoolean(1);
            arr[3] = Convert.ToBoolean(0);

            //printing values of each elements in the array
            foreach (bool element in arr)
            {
                Console.Write("{0} ",element);  
            }
            Console.ReadKey();
        }
    }
}

Output:
True False True False

Declaring, defining and initializing the array of boolean at the same time in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            bool[] arr = {true,false,Convert.ToBoolean(1),Convert.ToBoolean(0)};

            //printing values of each elements in the array
            foreach (bool element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}


Array of byte in C#

Sample code:

Declaring and defining the array of byte at the same time in C#
using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] arr = new byte[4];

            arr[0] = 10;
            arr[1] = 20;
            arr[2] = 5;
            arr[3] = unchecked((byte)300) ;

            //printing values of each elements in the array
            foreach (byte element in arr)
            {
                Console.Write("{0} ",element);  
            }
            Console.ReadKey();
        }
    }
}

Output:
10 20 5 44

Declaring and defining and initializing the array of byte at the same time in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] arr = {10,20,5,unchecked((byte)300)};

            //printing values of each element in the array
            foreach (byte element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}

Output:
10 20 5 44


Array of short in C#

Declaring and defining the array of short at the same time in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            short[] arr = new short[4];

            arr[0] = 10;
            arr[1] = 20;
            arr[2] = 5;
            arr[3] = unchecked((byte)300) ;

            //printing value of each element in the array
            foreach (short element in arr)
            {
                Console.Write("{0} ",element);  
            }
            Console.ReadKey();
        }
    }
}

Output:
10 20 5 44


Declaring, defining and initializing the array of short at same time in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            short[] arr = {10,20,5,unchecked((byte)300)};

            //printing  value of each elements in the array
            foreach (short element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}



Array of char in C#

Declaring and defining the array of character at the same time in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] arr = new char[4];

            arr[0] = 'A';
            arr[1] = 'b';
            arr[2] = (char)65;
            arr[3] = (char)97 ;

            //printing  value of each elements in the array
            foreach (char element in arr)
            {
                Console.Write("{0} ",element);  
            }
            Console.ReadKey();
        }
    }
}

Output:
A b A a

Declaring, defining and initializing the array of character at the same time in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] arr = {'A','b',(char)65,(char)97};

            //printing value of each element in the array
            foreach (char element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}

Array of int in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[3];

            arr[0]= 2;
            arr[1]=4;
            arr[2]=6;
          
            //printing value of each element in the array
            foreach (int element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}



Declaring, defining and initializing the array of integer at the same time in C#
using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = {2,4,6};

            //printing value of each element in the array
            foreach (int element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}

2 4 6

Array of long in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            long[] arr = new long[3];

            arr[0]= 2L;
            arr[1]=4L;
            arr[2]=6L;
          
            //printing value of each element in the array
            foreach (long element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}


Declaring, defining and initializing the array of long at the same time in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            long[] arr = {2L,4L,6L};

            //printing value of each element in the array
            foreach (long element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}

2 4 6







Array of interface in C#
Array of delegate in C#



No comments:

Post a Comment