use of custom generic class in C#

 

For Example: Write a program to use of custom generic class.

using System;

using System.Collections;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication12

{

    //Generic Class

    class GenericClass<MyType>

    {

        //Private Fields

        private MyType A, B;

        //Parameterized Constructor

        public GenericClass(MyType x, MyType y)

        {

            A = x;

            B = y;

        }

        public void Print()

        {

            Console.WriteLine("A={0},  B={1}", A, B);

        }

    }

    //Main Class

    class ProgramCall

    {

         static void Main(string[] args)

        {

            //Instantiating the genric class with int type

            GenericClass<int> intobject = new GenericClass<int>(10, 20);

            intobject.Print();

 

            //Instantiating the genric class with string type

            GenericClass<string> stringobject = new GenericClass<string>("Generic Method", "Generic Class");

            stringobject.Print();

            Console.ReadLine();

        }

    }

}

 

Output:

Comments

Popular posts from this blog

queue in C# Programming

collection classes in C#