stackoverflow1453

Programming&&Games&&Life

Generic Class – 1

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

namespace GenericComparator
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();

            bool result = p.Compare(3, 4);
            Console.WriteLine("result=" + result);

            ComparatorGeneric<int> a = new ComparatorGeneric<int>();
            Console.WriteLine(a.Compare(4, 4));

            ComparatorGeneric<string> s = new ComparatorGeneric<string>();
            Console.WriteLine(s.Compare("Ali", "Ali"));

            Console.ReadKey();
        }

        //Method Overloading
        bool Compare(int x, int y)
        {
            if (x.Equals(y))
            {
                return true;
            }
            else return false;
        }

        bool Compare(string x, string y)
        {
            if (x.Equals(y))
            {
                return true;
            }
            else return false;
        }
    }
    //Generic Class Implementation
    class ComparatorGeneric<T>
    {
        public bool Compare(T x, T y)
        {
            if (x.Equals(y))
            {
                return true;
            }

            else return false;
        }
    }
}
Advertisements

No comments yet»

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: