Sep 20, 2011

Array of float in C#

using System;

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

            arr[0]= 2.0f;
            arr[1]=2.5F;
            arr[2]=3.0F;
          
            //printing value of each element in the array
            foreach (float element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}

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

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            float[] arr = {2.0f,2.5F,3.0f};

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

2 2.5 3

Array of double in C#

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

using System;

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

            arr[0]= 2.0d;
            arr[1]=2.5D;
            arr[2]=3.0;
          
            //printing value of each element in the array
            foreach (double element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}


using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] arr = {2.0d,2.5D,3.0};

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

2 2.5 3

Array of decimal in C#

using System;

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

            arr[0]= 2.0m;
            arr[1]=2.5M;
            arr[2]=3.0M;
          
            //printing value of each element in the array
            foreach (decimal element in arr)
            {
                Console.Write("{0} ", element);
            }
            Console.ReadKey();
        }
    }
}


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

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal[] arr = {2.0m,2.5M,3.0M};

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

2.0 2.5 3.0

Array of struct in C#

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Student[] arr = new Student[3];
            arr[0] = new Student("Tom", 10, 50.0f);
            arr[1] = new Student("Jerry", 20, 80.5f);
            arr[2] = new Student("Davis", 30, 70.0f);

            //printing values of each elements in the array
            foreach (Student element in arr)
            {
                element.display();
            }
            Console.ReadKey();
        }
    }
}

struct Student
{
    string name;
    int roll;
    float marks;

    public Student(string name, int roll, float marks)
    {
        this.name = name;
        this.roll = roll;
        this.marks = marks;
    }
    public void display()
    {
        Console.WriteLine("{0} {1} {2}", roll, name, marks);
    }
}

10 Tom 50
20 Jerry 80.5
30 Davis 70

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Student[] arr = {new Student("Tom", 10, 50.0f),
                             new Student("Jerry", 20, 80.5f),
                             new Student("Davis", 30, 70.0f)};

            //printing values of each elements in the array
            foreach (Student element in arr)
            {
                element.display();
            }
            Console.ReadKey();
        }
    }
}

struct Student
{
    string name;
    int roll;
    float marks;

    public Student(string name, int roll, float marks)
    {
        this.name = name;
        this.roll = roll;
        this.marks = marks;
    }
    public void display()
    {
        Console.WriteLine("{0} {1} {2}", roll, name, marks);
    }
}

Array of enum in C#

Array of enum means array which contains only symbolic constants of that enum.

using System;

namespace arrayDemo
{
    class Program
    {
        enum Color { Red, Green, Blue };
        static void Main(string[] args)
        {
            Color[] arr = new Color[3];

            arr[0] = Color.Green;
            arr[1] = Color.Red;
            arr[2] = Color.Blue; 

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

In the above example array of enum arr can keep only Color.Red, Color.Green and Color.Blue

For any other value we have first type cast into Color. For example:
arr[0] = (Color)5;

Array of class in C#

Sample code:
using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Student[] arr = new Student[3];
            arr[0] = new Student("Tom",10,50.0f);
            arr[1] = new Student("Jerry", 20, 80.5f);
            arr[2] = new Student("Davis", 30, 70.0f);

            //printing values of each elements in the array
            foreach (Student element in arr)
            {
                element.display();
            }
            Console.ReadKey();
        }
    }
}

class Student
{
    private string name;
    private int roll;
    private float marks;

    public Student(string name,int roll,float marks)
    {
        this.name = name;
        this.roll = roll;
        this.marks = marks;
    }
    public void display(){
        Console.WriteLine("{0} {1} {2}",roll,name,marks);
    }
}

Output:
10 Tom 50
20 Jerry 80.5
30 Davis 70

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Student[] arr = {new Student("Tom", 10, 50.0f),
                             new Student("Jerry", 20, 80.5f),
                             new Student("Davis", 30, 70.0f)};

            //printing values of each elements in the array
            foreach (Student element in arr)
            {
                element.display();
            }
            Console.ReadKey();
        }
    }
}

class Student
{
    private string name;
    private int roll;
    private float marks;

    public Student(string name, int roll, float marks)
    {
        this.name = name;
        this.roll = roll;
        this.marks = marks;
    }
    public void display()
    {
        Console.WriteLine("{0} {1} {2}", roll, name, marks);
    }
}

Array of string in C#

using System;

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

            arr[0] = "exact";
            arr[1] = "help";
            arr[2] = "@blogspot.com";

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

using System;

namespace arrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] arr = {"exact","help","@blogspot.com"};
          

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

exacthelp@blogspot.com

string[] arr = new String[3]{"exact","help","@blogspot.com"};