C#循环通过数组

Que*_*CPO -1 c# arrays loops

我是C#的新手.我试图循环一个短数组,其中数组中的字符串元素放在网站搜索的末尾.代码:

int n = 1;
string[] s = {"firstitem","seconditem","thirditem"}
int x = s.Max(); // note, from my research this should return the maximum value in the array, but this is the first error
x = x + 1

while (n < x)
{

      System.Diagnostics.Process.Start("www.website.com/" + b[0]);

      b[]++; // this also generates an error "identifier expected"

}
Run Code Online (Sandbox Code Playgroud)

我的编码,逻辑或两者都是错的.根据我读过的内容,我应该能够在数组中获取最大值(作为int),然后添加到数组值,同时WHILE循环在网站末尾添加数组中的每个值(然后停止).请注意,在第一个错误上,我尝试使用不同的编码方式,如下所示:

int x = Convert.ToInt32(s.Max);
Run Code Online (Sandbox Code Playgroud)

但是,它会产生过载错误.如果我正确读取内容,MAX应该在序列中找到最大值.

And*_*dén 6

foreach(var str in s)
{
  System.Diagnostics.Process.Start("www.website.com/" + str);
}
Run Code Online (Sandbox Code Playgroud)

  • @Yatrix是不正确的,我不知道为什么你会关心这个代码中的这种微优化. (4认同)
  • @Yatrix不,不是.可读性在99.99%的情况下更为重要,除此之外:它不会更快.`foreach`确实*不*创建字符串的新副本. (3认同)
  • 我为什么要那样做? (2认同)