C#在自定义列表中存储多个字符串

时间:2016-12-07 11:41:21

标签: c# list

我需要存储多个字符串并分别检索每个字符串。

例如

string one = "String One";
string one = "String Two";
string one = "String Three";
string one = "String Four";

我目前正在使用自定义LIST(documentation)来执行此操作,但是,我很难找到存储所有这些变量的最有效方法并返回不同的字符串格式。

例如

    if(stringInput = "")
    {
       list<>.add(customMethod(one, two, three, ""));
    }
    else
    {
       list<>.add(customMethos(one, two, three, four));
    }

我不知道这是否是完成此任务的最佳方式。为每种返回类型创建列表类型会更有效吗?

我希望我已经清楚了!

1 个答案:

答案 0 :(得分:0)

尝试使用扩展方法创建自定义ToString(),https://msdn.microsoft.com/en-GB/library/bb383977.aspx?f=255&MSPPError=-2147217396

这将允许您使用普通列表来自定义输出。

public static class ExtensionMethods
{
    public static string MyToStringMethod(this IEnumerable<string> myNormalListOfStrings)
    {
         //my method
    }
}