”<” 操作员错误

发布于 2024-07-27 21:56:06 字数 1002 浏览 7 评论 0 原文

为什么 ( i < UniqueWords.Count ) 表达式在 for 循环中有效,但返回“CS0019 Operator '<' 无法应用于“int”类型和“方法组”类型的操作数”放入我的 if 中时出现错误? 它们都是先前声明的字符串数组。

for (int i = 0;i<UniqueWords.Count;i++){
        Occurrences[i] = Words.Where(x => x.Equals(UniqueWords[i])).Count();
        Keywords[i] = UniqueWords[i];
        if (i<UniqueURLs.Count) {rURLs[i] = UniqueURLs[i];}
}

编辑添加声明:

    List<string> Words = new List<string>();
    List<string> URLs = new List<string>();

//像这样添加元素。 。 。 。

            Words.Add (referringWords); //these are strings
            URLs.Add (referringURL);

        UniqueWords = Words.Distinct().ToList();
        UniqueURLs = URLs.Distinct().ToList();

已解决。 谢谢,方法 .Count() 需要括号,我仍然不完全理解为什么它们并不总是必要的。

Jon Skeet,谢谢,我想我也不明白声明到底是什么? 您想要分配的实际值吗? 它们是从外部源拉取的,但是是字符串。

我明白了! 谢谢。 (至少()。)

Why is the ( i < UniqueWords.Count ) expression valid in the for loop, but returns "CS0019 Operator '<' cannot be applied to operands of type 'int' and 'method group'" error when placed in my if? They are both string arrays, previously declared.

for (int i = 0;i<UniqueWords.Count;i++){
        Occurrences[i] = Words.Where(x => x.Equals(UniqueWords[i])).Count();
        Keywords[i] = UniqueWords[i];
        if (i<UniqueURLs.Count) {rURLs[i] = UniqueURLs[i];}
}

EDITED to add declarations:

    List<string> Words = new List<string>();
    List<string> URLs = new List<string>();

//elements added like so. . . .

            Words.Add (referringWords); //these are strings
            URLs.Add (referringURL);

        UniqueWords = Words.Distinct().ToList();
        UniqueURLs = URLs.Distinct().ToList();

SOLVED. thank you, parentheses were needed for method .Count() I still do not fully understand why they are not always necessary.

Jon Skeet, thanks, I guess I don't understand what exactly the declarations are either then? You wanted the actual values assigned? They are pulled from an external source, but are strings.

I get it! Thanks. (the ()'s at least.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

月竹挽风 2024-08-03 21:56:06

不要忘记你的父母:

UniqueWords.Count()

don't forget your parens:

UniqueWords.Count()
从此见与不见 2024-08-03 21:56:06

您绝对确定它们都是字符串数组吗?

您确定一个不是字符串数组,另一个不是 IEnumerable 吗? 这肯定能解释它。 (所讨论的方法组将是 Enumerable.Count() 扩展方法。)如果是这种情况,您也将无法在块内使用索引器。

如果不是这样,请删除任何无关的代码,但包含声明,以便我们有一个简短但完整的程序来测试。

事实上,根据您的编辑,它们不能被声明为字符串数组 - 因为您正在为它们分配List值。 我怀疑您会发现 UniqueWords 被声明为 ListIList,但是 UniqueURLs 声明为 IEnumerable。 如果您使用 var,这可能是隐式的。 将鼠标悬停在变量名称上即可找出类型 - 如果 var 带来的更多是诅咒而不是祝福,请返回显式键入变量。

Are you absolutely sure they're both string arrays?

Are you sure one isn't a string array, and the other an IEnumerable<string>? That would certainly explain it. (The method group in question would be the Enumerable.Count() extension method.) If this is the case, you won't then be able to use the indexer within the block, either.

If that's not it, please remove any extraneous code but include the declarations so that we have a short but complete program to test against.

In fact, given your edit, they can't be declared as string arrays - because you're assigning List<string> values to them. I suspect you'll find that UniqueWords is declared as List<string> or IList<string>, but UniqueURLs is declared as IEnumerable<string>. This may be implicit if you're using var though. Hover over the variable name to find out the type - and if var is being more of a curse than a blessing, go back to explicitly typing your variables.

晨曦÷微暖 2024-08-03 21:56:06

您需要在方法名称后面加上括号,这样它就不会将 i 与方法 Count 本身进行比较。

UniqueWords.Count()

You need the parens after the method name so that it's not comparing i to the method Count itself.

UniqueWords.Count()
肩上的翅膀 2024-08-03 21:56:06

UniqueURL 的类型是什么? 看来.Count不是一个属性,而是一个方法。

What's the type of UniqueURLs? It seems that .Count is not a property, but a method.

帝王念 2024-08-03 21:56:06

如果不知道您正在使用的对象的类型,就很难确定。 UniqueURLs 是否有 Count 属性,或者只有 Count() 扩展方法?

Without knowing the types of the objects you're working with, it's difficult to say for sure. Does UniqueURLs have a Count property, or only a Count() extension method?

凉栀 2024-08-03 21:56:06

我怀疑您的 UniqueURLs-collection 是 IEnumerable 而不是 ICollection 或衍生物,并且它没有 Count-property。 您正在调用 Enumerable.Count-extension 方法,使用括号就可以了:

if (i<UniqueURLs.Count()) {rURLs[i] = UniqueURLs[i];}

I would suspect that your UniqueURLs-collection is an IEnumerable rather than an ICollection or derivative and it doesn't have the Count-property. You're invoking the Enumerable.Count-extension method, use parenthesis and you'll be fine:

if (i<UniqueURLs.Count()) {rURLs[i] = UniqueURLs[i];}
筱果果 2024-08-03 21:56:06

已解决。 谢谢,括号里是
方法 .Count() 需要我仍然这样做
不完全理解为什么他们不
总是必要的。

乔恩·斯基特,谢谢,我想我不知道
明白到底是什么
那么声明是什么呢? 你
想要分配的实际值吗?
它们是从外部拉出的
源,但是是字符串。

我明白了! 谢谢。 (至少有 ()。)

区别如下:

声明为方法...

public class UniqueWords {
  public int Count() {
    // Code to get the count
  }
}

与括号一起使用:

if (i < UniqueWords.Count()) {}

如果声明为属性...

public class UniqueWords {
  public int Count {
    get {
        // code to get the count
    }
  }
}

则不带括号使用:

if (i < UniqueWords.Count) {}

因此,如果它是方法,请使用括号..如果它是财产,你就不需要。

SOLVED. thank you, parentheses were
needed for method .Count() I still do
not fully understand why they are not
always necessary.

Jon Skeet, thanks, I guess I don't
understand what exactly the
declarations are either then? You
wanted the actual values assigned?
They are pulled from an external
source, but are strings.

I get it! Thanks. (the ()'s at least.)

here's the difference:

Declared as a method...

public class UniqueWords {
  public int Count() {
    // Code to get the count
  }
}

is used with parens:

if (i < UniqueWords.Count()) {}

If declared as a property...

public class UniqueWords {
  public int Count {
    get {
        // code to get the count
    }
  }
}

is used without parens:

if (i < UniqueWords.Count) {}

So, if it's a method, use parens.. if it's a property, you don't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文