Monday, 28 December 2015

C# Program to Demonstrate Boxing Operations

using System;
class sample
{
    int x = 10;
    object obj;
    void boxmethod()
    {
        sample s= new sample();
        bool b;
        object ob="CSHARP";
        b=s.obj is int;
        Console.WriteLine(b);
        s.obj = x;
        b = s.obj is int;
        Console.WriteLine("{0},{1},{2}",s.obj,s.x,b);
        s.x = (int)s.obj;
        s.x = 20;
        b = s.obj is int;
        Console.WriteLine("{0},{1},{2}", s.obj, s.x, b);
        s.obj="CSHARP";
        b=s.obj is int;
        Console.WriteLine("{0},{1},{2}",s.obj,s.x,b);
        Console.ReadLine();
    }
    public static void Main()
    {
        sample s=new sample();
        s.boxmethod();
    }
}
--------------------
OUTPUT:-

False
10,10,True
10,20,True
CSHARP,20,False
---------------------------------------------

Saturday, 26 December 2015

How to Combine Two Delegates

using System;
delegate void dele(string s);
class TestClass
{
    static void Good(string s)
    {
        System.Console.WriteLine("  Good, {0}!", s);
    }

    static void Morning(string s)
    {
        System.Console.WriteLine("  Morning, {0}!", s);
    }

    static void Main()
    {
        dele firstdel, secondDel, multiDel, multiMinusfirstdel;
        firstdel = Good;
        secondDel = Morning;
        multiDel = firstdel + secondDel;
        multiMinusfirstdel = multiDel - firstdel;
        Console.WriteLine("Invoking delegate firstdel:");
        firstdel("A");
        Console.WriteLine("Invoking delegate secondDel:");
        secondDel("B");
        Console.WriteLine("Invoking delegate multiDel:");
        multiDel("C");
        Console.WriteLine("Invoking delegate multiMinusfirstdel:");
        multiMinusfirstdel("D");
        Console.ReadLine();
    }
}
--------------
OUTPUT:-

Invoking delegate firstDel:
  Good, A!
Invoking delegate SecondDel:
  Morning, B!
Invoking delegate multiDel:
  Good, C!
  Morning, C!
Invoking delegate multiMinusFirstDel:
  Morning, D!
------------

In C# Program How to get the Length of the Array

/*
 * C# Program to get the Length of the Array
 */
using System;
class Program
{
    static void Main()
    {

        int[] arrayA = new int[5];
        int lengthA = arrayA.Length;
        Console.WriteLine("Length of ArrayA : {0}", +lengthA);
        long longLength = arrayA.LongLength;
        Console.WriteLine("Length of the LongLength Array  : {0}", longLength);
        int[,] twoD = new int[5, 10];
        Console.WriteLine("The Size of 2D Array is : {0}", twoD.Length);
        Console.ReadLine();
    }
}

----------------
OUTPUT:-

Length of ArrayA : 5
Length of the LongLength Array is :5
The Size of 2D Array is : 50
---------------------

C# Program to Convert a 2D Array into 1D Array

/*
 * C# Program to Convert a 2D Array into 1D Array
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
    class twodmatrix
    {
        int m, n;
        int[,] a;
        int[] b;
        twodmatrix(int x, int y)
        {
            m = x;
            n = y;
            a = new int[m, n];
            b = new int[m * n];
        }
        public void readmatrix()
        {
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.WriteLine("a[{0},{1}]=", i, j);
                    a[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public void printd()
        {
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write("{0}\t", a[i, j]);

                }
                Console.Write("\n");
            }
        }
        public void convert()
        {
            int k = 0;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    b[k++] = a[i, j];
                }
            }
        }
        public void printoned()
        {
            for (int i = 0; i < m * n; i++)
            {
                Console.WriteLine("{0}\t", b[i]);
            }
        }


        public static void Main(string[] args)
        {
            twodmatrix obj = new twodmatrix(2, 3);
            Console.WriteLine("Enter the Elements : ");
            obj.readmatrix();
            Console.WriteLine("\t\t Given 2-D Array(Matrix) is : ");
            obj.printd();
            obj.convert();
            Console.WriteLine("\t\t Converted 1-D Array is : ");
            obj.printoned();
            Console.ReadLine();
        }
    }
}
--------------------------
OUTPUT:-

Enter the Elements :
a[0,0]= 1
a[0,1]= 2
a[0,2]= 3
a[1,0]= 5
a[1,1]= 4
a[1,2]= 5

                 Given 2-D Array(Matrix) is :
1       2       3
5       4       5

                 Converted 1-D Array is :
1
2
3
5
4
5
4

---------------