如何在C#中修复这些错误?

时间:2011-09-05 11:12:47

标签: c# .net compiler-errors

这是我遇到错误的代码:

this.terminchar = Convert.ToChar(8080);    
List<string> source = new List<string>(this.base64Decode(parse_value).Split(new char[] { this.terminchar }));

if ((source) % 2) == 0)
{
  for (int i = 0; i < source; i++)
  {
    this.keys.Add(source[i], source[++i]);
  }
}

我在这段代码中遇到3个错误,第一个错误:

  

错误1运算符'&lt;'不能应用于'int'和'System.Collections.Generic.List'

类型的操作数

第二个:

  

错误2运算符'%'无法应用于'System.Collections.Generic.List'和'int'类型的操作数

第三个:

  

错误3无效的表达式术语'=='

我是C#的新手,这是我的朋友源代码,我只是想了解语法,但我不知道该怎么做。任何给予的帮助将不胜感激,谢谢。

5 个答案:

答案 0 :(得分:6)

在这两种情况下,您可能都在寻找.Count属性。

所以请使用source.Count

答案 1 :(得分:3)

您正在对列表执行某些操作。我很确定,你应该按照以下方式行......

if ((source.Count) % 2) == 0)  

for (int i = 0; i < source.Count; i++)

代替

答案 2 :(得分:1)

显然,你可以在那里使用for循环。使用i<source.Count(source.Count) % 2代替

答案 3 :(得分:0)

您需要在源上使用.Count来获取列表中的项目数

List<string> source = new List<string>(this.base64Decode(parse_value).Split(new    char[] { Convert.ToChar(8080) }));
string Command = source[0];
source.RemoveAt(0);
if ((source.Count) % 2) == 0)
{
    for (int i = 0; i < source.Count; i++)
    {
        this.keys.Add(source[i], source[++i]);
    }
}

答案 4 :(得分:0)

sourceList<string> - 字符串的容器,请参阅MSDN

运算符<%可以应用于int。所以你的代码遗漏了一些东西。