C# Generic class with list collection or Generic Class Demo

Program

using System;
using System.Collections.Generic;

namespace Collections
{
    class GenericClassExample
    {
        class GetAndSort<T>
        {
            public void SortAndDisplay(List<T> list)
            {
                list.Sort();
                foreach (T item in list)
                {
                    Console.WriteLine(item);
                }
            }
        }

        public static void Main(string[] args)
        {
            List<string> Trainees = new List<string>() 
                                   { "Sneha", "Pavithra",
                                     "Nandhini", "Raj Nandhini"
                                     "Divya", "Nagarjun", "Stephin"};

            //Consuming GetAndSort for string type.
            GetAndSort<string> gs1 = new GetAndSort<string>();
            gs1.SortAndDisplay(Trainees);

            //Consuming GetAndSort for integer type. 
            List<int> Marks = new List<int>() {98,56,88,42,96,83,60};
            GetAndSort<int> gs2 = new GetAndSort<int>();
            gs2.SortAndDisplay(Marks);

            Console.ReadKey();

        }//main..

    }//class..

}//namespace..



Output

Divya
Nagarjun
Nandhini
Pavithra
Raj Nandhini
Sneha
Stephin

42
56
60
83
88
96

98

No comments:

Post a Comment