Click here to Skip to main content
15,798,592 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I need code for a generic method that accepts three arguments of the same type and displays them in order. for numeric data, "in order" means in numeric order. For other classes, define "in order" appropriately.

Please help, I have litle understanding on generics and this is from a question paper I came across while trying to learn them.
Posted

1 solution

You don't even need generics - you can do it with a simple method and the application of inheritance to overridding:
C#
private void PrintOrder(params object[] args)
    {
    Array.Sort(args);
    foreach (object o in args)
        {
        Console.WriteLine(o);
        }
    }


Then:
C#
PrintOrder(17, 8, 42);
PrintOrder("hello", "there", "alpha", "bravo");
PrintOrder(new DateTime(2012, 12, 31), new DateTime(2012, 1, 31), DateTime.Now);

Will produce:
8
17
42
alpha
bravo
hello
there
31/01/2012 00:00:00
15/07/2012 15:40:13
31/12/2012 00:00:00
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900