C# 以递归方式打印集合的效果非常好
我已经看到了很多关于这个的问题,但是我没有看到任何关于递归的问题。我已经创建了一些扩展方法,它们在深度为1的情况下可以很好地进行打印。看起来是这样的:C# 以递归方式打印集合的效果非常好,c#,C#,我已经看到了很多关于这个的问题,但是我没有看到任何关于递归的问题。我已经创建了一些扩展方法,它们在深度为1的情况下可以很好地进行打印。看起来是这样的: public static string PrettyToString<K, V>(this Dictionary<K, V> dictionary) { string result = "{"; foreach (var kvp in dictionary)
public static string PrettyToString<K, V>(this Dictionary<K, V> dictionary)
{
string result = "{";
foreach (var kvp in dictionary)
{
result += $"({kvp.Key}, {kvp.Value}) ";
}
result += "}";
return result;
}
public static string PrettyToString<T>(this List<T> list)
{
string result = "{";
foreach (var element in list)
{
result += $"{element}, ";
}
result += "}";
return result;
}
public static string PrettyToString<T>(this T [] list)
{
string result = "{";
foreach (var element in list)
{
result += $"{element}, ";
}
result += "}";
return result;
}
{(foo, System.Collections.Generic.Dictionary`2[System.String,System.Boolean])...
我希望它看起来像这样:
{(foo, {(bar, true)})...
我正在寻找递归打印的方法,而不考虑嵌套类型:
var x = new List<Dictionary<string, string>>();
var y = new Dictionary<Dictionary<string, string>, string>>();
var z = new Dictionary<Dictionary<string, string>, List<string>>>();
...
x.PrettyToString();
y.PrettyToString();
z.PrettyToString();
var x=新列表();
var y=新字典>();
var z=新字典>();
...
x、 PrettyToString();
y、 PrettyToString();
z、 PrettyToString();
都应该递归地打印出内容。如何实现这一点?我更改了方法的签名,并使它们成为非泛型的 诀窍是在转换之前确定类型 请看下面的示例代码。我希望有帮助 请查看源代码底部的Ext类
使用系统;
使用系统集合;
使用System.Collections.Generic;
名称空间控制台App8
{
班级计划
{
静态void Main(字符串[]参数)
{
var Dic=新字典{{1,“Ali”},{2,“B”};
Console.WriteLine(Dic.PrettyToString());
var Dic1=新字典{{“Ali”,12.5f},{“B”,99.9f};
Console.WriteLine(Dic1.PrettyToString());
var Dic2=新字典
{
{新列表{1,2,3},“A”},
{新名单{4,5,6},“B”}
};
Console.WriteLine(Dic2.PrettyToString());
var Dic3=新字典
{
{新词典{{“a”,“a”},{“b”,“b”},{“大写”},
{新词典{{“1”、“1”}、{“2”、“4”}、{“4”、“16”}、{“Power”}
};
Console.WriteLine(Dic3.PrettyToString());
var Dic4=新字典
{
{新词典{{“a”,“a”},{“b”,“b”},新列表{“a”,“b”},
{新词典{{“1”,“1”},{“2”,“4”},{“4”,“16”},新列表{“1”,“2”,“4”}
};
Console.WriteLine(Dic4.PrettyToString());
var L=新列表
{
新名单{1,2,3},
新名单{4,5,6}
};
Console.WriteLine(L.PrettyToString());
Console.ReadKey();
}
}
静态类扩展
{
公共静态字符串PrettyToString(此IDictionary字典)
{
字符串结果=“{”;
foreach(dictionary.Keys中的var键)
{
result+=string.Format(({0},{1})”,PrettyToString(Key),PrettyToString(dictionary[Key]);
}
结果+=“}”;
返回结果;
}
公共静态字符串PrettyToString(此IEnumerable列表)
{
字符串结果=“{”;
foreach(列表中的var元素)
{
result+=string.Format(“{0},”,PrettyToString(元素));
}
结果+=“}”;
返回结果;
}
私有静态字符串PrettyToString(对象O)
{
var S=O作为字符串;
如果(S!=null)返回S;
var D=O作为索引;
如果(D!=null),则返回D.PrettyToString();
var L=O作为IEnumerable;
如果(L!=null),则返回L.PrettyToString();
返回O.ToString();
}
}
}
您的第二个方法可以更改为接受IEnumerable
,而无需修改该方法的主体,从而允许它支持其他集合类型,例如T[]
和HashSet。这将使我们摆脱多余的第三种方法。@Amy谢谢你的提示:)感谢你对否决权的评论!我没有投你反对票,但这段代码没有在我的编辑器中编译。这可能就是你被否决的原因。我使用了C#7模式匹配功能。我想这就是为什么会出现编译错误。请尝试更新的示例代码。此外,还有一个指向.NET fiddle online editor的链接,该编辑器工作正常。我的错误在PrettyToString签名上。e、 它说我需要字典的类型参数。更新:啊!我需要使用System.Collections
而不是System.Collections.Generic
。直到现在我才意识到区别。
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApp8
{
class Program
{
static void Main(string[] args)
{
var Dic = new Dictionary<int, string> { { 1, "Ali" }, { 2, "B" } };
Console.WriteLine(Dic.PrettyToString());
var Dic1 = new Dictionary<string, float> { { "Ali", 12.5f }, { "B", 99.9f } };
Console.WriteLine(Dic1.PrettyToString());
var Dic2 = new Dictionary<List<int>, string>
{
{ new List<int> { 1, 2, 3 }, "A" },
{ new List<int> { 4, 5, 6 }, "B" }
};
Console.WriteLine(Dic2.PrettyToString());
var Dic3 = new Dictionary<Dictionary<string, string>, string>
{
{ new Dictionary<string, string> { { "a", "A" }, { "b", "B" } }, "Capital" },
{ new Dictionary<string, string> { { "1", "1" }, { "2", "4" }, { "4", "16" } }, "Power" }
};
Console.WriteLine(Dic3.PrettyToString());
var Dic4 = new Dictionary<Dictionary<string, string>, List<string>>
{
{ new Dictionary<string, string> { { "a", "A" }, { "b", "B" } }, new List<string> { "A", "B" } },
{ new Dictionary<string, string> { { "1", "1" }, { "2", "4" }, { "4", "16" } }, new List<string> { "1", "2", "4" } }
};
Console.WriteLine(Dic4.PrettyToString());
var L = new List<List<int>>
{
new List<int> { 1, 2, 3 },
new List<int> { 4, 5, 6 }
};
Console.WriteLine(L.PrettyToString());
Console.ReadKey();
}
}
static class Ext
{
public static string PrettyToString(this IDictionary dictionary)
{
string result = "{";
foreach (var Key in dictionary.Keys)
{
result += string.Format("({0}, {1}) ", PrettyToString(Key), PrettyToString(dictionary[Key]));
}
result += "}";
return result;
}
public static string PrettyToString(this IEnumerable list)
{
string result = "{";
foreach (var element in list)
{
result += string.Format("{0}, ", PrettyToString(element));
}
result += "}";
return result;
}
private static string PrettyToString(object O)
{
var S = O as string;
if (S != null) return S;
var D = O as IDictionary;
if (D != null) return D.PrettyToString();
var L = O as IEnumerable;
if (L != null) return L.PrettyToString();
return O.ToString();
}
}
}