【问题标题】:how to convert type 'string' to 'string[]' in c#?如何在 C# 中将类型“字符串”转换为“字符串 []”?
【发布时间】:2015-03-10 19:36:01
【问题描述】:

我的代码有点问题,请您帮忙:

         String[] Boe;

         Boe = new String[1]; <---- i think the error might also be here 
         BS = new Rectangle();
         for (int p = 0; p < 1; p++)
         {
           //some code have been taken out 

             Boe = "Yes"; <----- this is where the error is being displayed 
         }

【问题讨论】:

  • Boe[0] = "Yes" 并且这里真的不需要for 循环。

标签: c# xna


【解决方案1】:

您的代码的问题是您试图将整个数组设置为一个简单的字符串。您必须访问数组的索引,然后给这个位置一个字符串值。 往下看

 String[] Boe;
 Boe = new String[1]; <---- i think the error might also be here 
 BS = new Rectangle();
 for (int p = 0; p < 1; p++)
 {
   //some code have been taken out 

     Boe[p] = "Yes"; <----- this is where the error is being displayed 
 }

希望对你有帮助!

【讨论】:

  • 在这种情况下,我给了你一个 for 循环,但 @AndrejBratoz 只给你索引器 0 是正确的,因为你只有一个值。您将失去一些性能,因为在 for 循环结束时,您将再次检查您是否仍在范围内。如果示例代码只是一个示例,并且您可能在不久的将来在您的软件中需要它,则给出了循环。
猜你喜欢
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2023-02-08
  • 2021-03-30
  • 2012-09-19
相关资源
最近更新 更多