”<” 操作员错误
为什么 ( 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,谢谢,我想我也不明白声明到底是什么? 您想要分配的实际值吗? 它们是从外部源拉取的,但是是字符串。
我明白了! 谢谢。 (至少()。)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不要忘记你的父母:
don't forget your parens:
您绝对确定它们都是字符串数组吗?
您确定一个不是字符串数组,另一个不是
IEnumerable
吗? 这肯定能解释它。 (所讨论的方法组将是Enumerable.Count()
扩展方法。)如果是这种情况,您也将无法在块内使用索引器。如果不是这样,请删除任何无关的代码,但包含声明,以便我们有一个简短但完整的程序来测试。
事实上,根据您的编辑,它们不能被声明为字符串数组 - 因为您正在为它们分配
List
值。 我怀疑您会发现UniqueWords
被声明为List
或IList
,但是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 theEnumerable.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 thatUniqueWords
is declared asList<string>
orIList<string>
, butUniqueURLs
is declared asIEnumerable<string>
. This may be implicit if you're usingvar
though. Hover over the variable name to find out the type - and ifvar
is being more of a curse than a blessing, go back to explicitly typing your variables.您需要在方法名称后面加上括号,这样它就不会将 i 与方法 Count 本身进行比较。
You need the parens after the method name so that it's not comparing i to the method Count itself.
UniqueURL 的类型是什么? 看来.Count不是一个属性,而是一个方法。
What's the type of UniqueURLs? It seems that .Count is not a property, but a method.
如果不知道您正在使用的对象的类型,就很难确定。 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?
我怀疑您的 UniqueURLs-collection 是 IEnumerable 而不是 ICollection 或衍生物,并且它没有 Count-property。 您正在调用 Enumerable.Count-extension 方法,使用括号就可以了:
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:
区别如下:
声明为方法...
与括号一起使用:
如果声明为属性...
则不带括号使用:
因此,如果它是方法,请使用括号..如果它是财产,你就不需要。
here's the difference:
Declared as a method...
is used with parens:
If declared as a property...
is used without parens:
So, if it's a method, use parens.. if it's a property, you don't.