在这里如何使用泛型? [英] How to use generics here?

查看:83
本文介绍了在这里如何使用泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用List<t></t>将这两种方法合而为一. 怎么做?

当前方法如下:

I want to join these two methods into one using List<t></t>.
How to do it?

Current methods look as follows:

protected string JoinList(List<int> list)
        {
            string result = string.Empty;
            if (list != null && list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    result += list[i].ToString();
                    result += (i != list.Count - 1) ? "," : string.Empty;
                }
            }
            return result;
        }
        protected string JoinList(List<string> list)
        {
            string result = string.Empty;
            if (list != null && list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    result += list[i];
                    result += (i != list.Count - 1) ? "," : string.Empty;
                }
            }
            return result;
        }

推荐答案

以下是为您提供的一个小示例:
Here is a little example for you:
// Our generic method
protected static string JoinList<T>(List<T> list)
{
    string result = string.Empty;
    if (list != null && list.Count > 0)
    {
        for (int i = 0; i < list.Count; i++)
        {
            result += list[i].ToString();
            result += (i != list.Count - 1) ? "," : string.Empty;
        }
    }
    return result;
}

// Test the method
static void Main(string[] args)
{
    // Prepare to lists
    List<int> lstInt = new List<int>();
    List<string> lstString = new List<string>();
    lstInt.Add(1);
    lstInt.Add(2);
    lstInt.Add(3);
    lstString.Add("one");
    lstString.Add("two");
    lstString.Add("three");

    // And the results...
    Console.WriteLine(JoinList<int>(lstInt));
    Console.WriteLine(JoinList<string>(lstString));
    Console.ReadKey();
}



有关泛型方法的更多信息,请参见此处 [



For more information about generic methods see here[^]. :)


除了使方法静态化之外,这是方法的改进版本.

Beyond making the method static, here''s an improved version of your method.

public static string JoinList(List<int> list)
{
    // start with an empty string
    StringBuilder builder = new StringBuilder("");
    if (list != null)
    {
        // build the string
        foreach(int number in list)
        {
            builder.AppendFormat(",{0}", number);
        }
        // remove the leading comma
        if (builder.Length > 0)
        {
            builder.RemoveAt(0);
        }
    }
    return builder.ToString();
}</int>



使用StringBuilder效率更高,因为它不会在您每次向字符串添加内容时都重新分配该字符串.

另外,在foreach循环中,我可以检查一下每个项目的字符串是否为空,以避免在字符串的开头出现逗号,但是继续进行创建会更有效.字符串,然后在字符串构建完成后处理一次逗号.仅仅因为当今计算机有很多内存并不能免除我们使代码尽可能高效的责任.



Using the StringBuilder is more efficient because it doesn''t reallocate the string every time you add something to it.

Also, in the foreach loop, I could have checked to see if the string was empty for each item to avoid having the comma show up at the beginning of the string, but it''s more efficient just to go ahead and create the string, and then take care of the leading comma one time after the string has been built. Just because computers have a lot of memory nowadays doesn''t remove our responsibility for making the code as efficient as possible.


这篇关于在这里如何使用泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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