接受三个参数的通用方法 [英] Generic method that accepts three arguments

查看:54
本文介绍了接受三个参数的通用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要一个通用方法的代码,该方法接受相同类型的三个参数并按顺序显示它们.对于数字数据,按顺序"表示按数字顺序.对于其他类,请适当地定义按顺序".

请帮忙,我对泛型有一点了解,这是我在尝试学习泛型时遇到的一个问题文件.

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.

推荐答案

您甚至不需要泛型-您可以使用简单的方法以及将继承应用于覆盖的方法来做到这一点:
You don''t even need generics - you can do it with a simple method and the application of inheritance to overridding:
private void PrintOrder(params object[] args)
    {
    Array.Sort(args);
    foreach (object o in args)
        {
        Console.WriteLine(o);
        }
    }



然后:



Then:

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


这篇关于接受三个参数的通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆