Saturday, October 5, 2013

Generic Collections basic Swaping

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demos
{
    class clsGenericClass
    {
        //Set this to start up first
        static void Main()
        {
            int a = 10, b = 20;

            Console.WriteLine("a={0},b={1}", a, b);

            // Non Generic Pass by refence of type int
            Swap(ref a, ref  b);

            //Using Geneirc Type of type int
            //  Swap<int>(ref a,ref b);

            Console.WriteLine("a={0},b={1}", a, b);



            //Generic Method calling of type string
            string s1 = "Venu", s2 = "Gopal";
            Console.WriteLine("a={0},b={1}", s1, s2);
            //String type
            Swap<string>(ref s1, ref s2);
            Console.WriteLine("a={0},b={1}", s1, s2);


            Console.ReadLine();

        }
        //by pass by refentce of type  int
        private static void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;

        }
        //But for multiple types of values we use generics
        private static void Swap<T>(ref T a, ref T b)
        {
            T temp = a;
            a = b;
            b = temp;
        }
    }
}


No comments: