Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c将列表转换为字符串并显示在windows窗体应用程序的标签中_C#_String_Forms_List - Fatal编程技术网

C# c将列表转换为字符串并显示在windows窗体应用程序的标签中

C# c将列表转换为字符串并显示在windows窗体应用程序的标签中,c#,string,forms,list,C#,String,Forms,List,所以我有一个随机生成的字符列表,我想在标签中显示列表的内容。新短语生成一组新的随机字符,并将它们保存到列表中。下面是我试图用来显示标签的代码: public Form1() { InitializeComponent(); Game.NewPhrase(); phraseLbl.Text = Game.code.ToString(); } 但是在标签框中,随机字符应该显示为System.Collections.Generic.List'1[System.Char]。我不

所以我有一个随机生成的字符列表,我想在标签中显示列表的内容。新短语生成一组新的随机字符,并将它们保存到列表中。下面是我试图用来显示标签的代码:

public Form1()
{
    InitializeComponent();
    Game.NewPhrase();
    phraseLbl.Text = Game.code.ToString();
}
但是在标签框中,随机字符应该显示为System.Collections.Generic.List'1[System.Char]。我不确定这告诉了我什么。

只需使用字符串构造函数接受字符数组:

只需使用字符串构造函数接受字符数组:

也可以使用string.Concat

输出

但是,这就是为什么不使用string.Concat

基准 测试了1000倍于每个规模大小的char数组,在每次运行之前收集垃圾,并统计了前75%的结果

┌──────────────────┬────────────────────────────────────────────┐
│        Test Mode │ Release (64Bit)                            │
│   Test Framework │ .NET Framework 4.7.1 (CLR 4.0.30319.42000) │
╞══════════════════╪════════════════════════════════════════════╡
│ Operating System │ Microsoft Windows 10 Pro                   │
│          Version │ 10.0.17763                                 │
├──────────────────┼────────────────────────────────────────────┤
│       CPU System │ Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz    │
│  CPU Description │ Intel64 Family 6 Model 158 Stepping 9      │
├──────────────────┼──────────┬──────────────────┬──────────────┤
│  Cores (Threads) │ 4 (8)    │     Architecture │ x64          │
│      Clock Speed │ 3600 MHz │        Bus Speed │ 100 MHz      │
│          L2Cache │ 1 MB     │          L3Cache │ 8 MB         │
└──────────────────┴──────────┴──────────────────┴──────────────┘
结果

代码

也可以使用string.Concat

输出

但是,这就是为什么不使用string.Concat

基准 测试了1000倍于每个规模大小的char数组,在每次运行之前收集垃圾,并统计了前75%的结果

┌──────────────────┬────────────────────────────────────────────┐
│        Test Mode │ Release (64Bit)                            │
│   Test Framework │ .NET Framework 4.7.1 (CLR 4.0.30319.42000) │
╞══════════════════╪════════════════════════════════════════════╡
│ Operating System │ Microsoft Windows 10 Pro                   │
│          Version │ 10.0.17763                                 │
├──────────────────┼────────────────────────────────────────────┤
│       CPU System │ Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz    │
│  CPU Description │ Intel64 Family 6 Model 158 Stepping 9      │
├──────────────────┼──────────┬──────────────────┬──────────────┤
│  Cores (Threads) │ 4 (8)    │     Architecture │ x64          │
│      Clock Speed │ 3600 MHz │        Bus Speed │ 100 MHz      │
│          L2Cache │ 1 MB     │          L3Cache │ 8 MB         │
└──────────────────┴──────────┴──────────────────┴──────────────┘
结果

代码


谢谢你的回复!尝试一下,它成功地打印出了第一个字母,但是剩下的9个字母在列表中总共不显示10个字符。我测试了看,并确保我的方法实际上是用10填充,它是。@Yummy275你说的完全不可能。它不能在你的机器上以不同的方式运行,在我的机器上它可以正确地创建字符串。确保Game.code是一个包含所有条目的列表,只需在我建议的那一行上放置一个断点即可。@Yummy275是否有空字符?不应该matter@MichałTurczyn你是对的,我忘了使用实际设置列表长度的方法lol。再次感谢!它现在工作得很好!如果向列表中添加项目,则不必设置列表的长度。ToArray应返回一个包含所有项目的数组。感谢您的回复!尝试一下,它成功地打印出了第一个字母,但是剩下的9个字母在列表中总共不显示10个字符。我测试了看,并确保我的方法实际上是用10填充,它是。@Yummy275你说的完全不可能。它不能在你的机器上以不同的方式运行,在我的机器上它可以正确地创建字符串。确保Game.code是一个包含所有条目的列表,只需在我建议的那一行上放置一个断点即可。@Yummy275是否有空字符?不应该matter@MichałTurczyn你是对的,我忘了使用实际设置列表长度的方法lol。再次感谢!它现在工作得很好!如果向列表中添加项目,则不必设置列表的长度。ToArray应该返回一个包含所有项目的数组。令人印象深刻!出于好奇,您是如何获得性能详细信息的?@MichałTurczyn我一直在编写自己的基准测试程序来帮助我完成工作。它是专门为我工作的环境设计的,但它特别针对Stackoverflow输出进行了调整:P不过我建议,事实上,它在很多方面都更好,而且更具可重构性。太棒了!谢谢:令人印象深刻!出于好奇,您是如何获得性能详细信息的?@MichałTurczyn我一直在编写自己的基准测试程序来帮助我完成工作。它是专门为我工作的环境设计的,但它特别针对Stackoverflow输出进行了调整:P不过我建议,事实上,它在很多方面都更好,而且更具可重构性。太棒了!谢谢:
sff
┌──────────────────┬────────────────────────────────────────────┐
│        Test Mode │ Release (64Bit)                            │
│   Test Framework │ .NET Framework 4.7.1 (CLR 4.0.30319.42000) │
╞══════════════════╪════════════════════════════════════════════╡
│ Operating System │ Microsoft Windows 10 Pro                   │
│          Version │ 10.0.17763                                 │
├──────────────────┼────────────────────────────────────────────┤
│       CPU System │ Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz    │
│  CPU Description │ Intel64 Family 6 Model 158 Stepping 9      │
├──────────────────┼──────────┬──────────────────┬──────────────┤
│  Cores (Threads) │ 4 (8)    │     Architecture │ x64          │
│      Clock Speed │ 3600 MHz │        Bus Speed │ 100 MHz      │
│          L2Cache │ 1 MB     │          L3Cache │ 8 MB         │
└──────────────────┴──────────┴──────────────────┴──────────────┘
┌── Standard input ────────────────────────────────────────────────────────────┐
│ Value  │    Average │    Fastest │    Cycles │ Garbage │ Test │         Gain │
├── Scale 10 ───────────────────────────────────────────────────── 0.103 sec ──┤
│ new    │   1.492 µs │ 800.000 ns │   8.408 K │ 0.000 B │ Base │       0.00 % │
│ Concat │   3.128 µs │   1.600 µs │  14.210 K │ 0.000 B │ Pass │    -109.65 % │
├── Scale 100 ──────────────────────────────────────────────────── 0.095 sec ──┤
│ new    │   1.501 µs │ 800.000 ns │   8.474 K │ 0.000 B │ Base │       0.00 % │
│ Concat │   3.772 µs │   2.700 µs │  16.621 K │ 0.000 B │ Pass │    -151.23 % │
├── Scale 1,000 ────────────────────────────────────────────────── 0.098 sec ──┤
│ new    │   1.932 µs │ 800.000 ns │   9.949 K │ 0.000 B │ Base │       0.00 % │
│ Concat │  18.724 µs │  15.400 µs │  70.817 K │ 0.000 B │ Pass │    -868.93 % │
├── Scale 10,000 ───────────────────────────────────────────────── 0.123 sec ──┤
│ new    │   4.843 µs │   1.700 µs │  21.343 K │ 0.000 B │ Base │       0.00 % │
│ Concat │ 143.127 µs │ 140.900 µs │ 518.406 K │ 0.000 B │ Pass │  -2,855.53 % │
├── Scale 100,000 ──────────────────────────────────────────────── 0.306 sec ──┤
│ new    │  14.013 µs │  13.900 µs │  53.592 K │ 0.000 B │ Base │       0.00 % │
│ Concat │   1.421 ms │   1.378 ms │   5.118 M │ 0.000 B │ Pass │ -10,041.75 % │
└──────────────────────────────────────────────────────────────────────────────┘
[Test("new", "", true)]
public string Test1(char[] input, int scale)
{
   return new string(input);

}

[Test("Concat", "", false)]
public string Test2(char[] input, int scale)
{
   return string.Concat(input);
}