Examples with Generic Method & Class



Example  for Generic Method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Program for Generic Method
namespace CAGenerics
{
    class ClsGeneric
    {
        static void Display<G>(G S)
        {
            Console.WriteLine("Value is " + S);
        }
        static void Main(string[] args)
        {
            Display<string>("Welcome");
            Display<int>(9);
            Display<double>(9.9);
            Console.Read();
        }
    }
}



Output :-



Example  for Generic Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Program for Generic Class
namespace CAGenerics
{
    class ClsSample<TP>
    {
        public void Display(TP S)
        {
            Console.WriteLine("Value is  :-  " + S);
        }
    }
    class ClsGeneric2
    {
        static void Main()
        {
            ClsSample<string> ObjS = new ClsSample<string>();
            ClsSample<int> ObjI = new ClsSample<int>();
            ClsSample<double> ObjD = new ClsSample<double>();
            ObjS.Display("Sampath kumar vaddepally");
            ObjI.Display(9);
            ObjD.Display(99.9);
            Console.Read();
        }
    }
}









Output :-


No comments:

Post a Comment