Operator cannot be applied to operands of type 'Method Group' and 'int'
Solution 1
It should be Length
not Count
for arrays:
string [] Quantitys = Program.RecieptList[i].ItemQuantitys.Split(new char[] {'*'});
for (int ii = 0; i <= Quantitys.Length - 1; ii++)
{
}
More information on the MSDN: Array.Length
Also, unless it was intentional, your ii
should just be i
in your for
loop:
for (int i = 0; i <= Quantitys.Length - 1; i++)
Although, as was pointed out in the comments below, you could also use the Quantitys.Count()
, since arrays inherit from IEnumerable<T>
. Personally, though, for one-dimensional arrays, I prefer to use the standard Array.Length
property.
Solution 2
Simply add brackets (). Count() works for me now as well
Solution 3
Arrays have a Length
property, not a Count
property, though they do the same thing. The error you're seeing is because there's an extension method Count()
that it's finding instead, but can't quite use because you didn't invoke it with ()
. You could use your array as an IList<T>
instead so that you can keep the familiar Count
property name.
Also, i
and ii
will be confusing to most people (including yourself, from the looks of it: you included both in your for
line). The standard in programming, carried over from mathematics, is i
, j
, k
, ... for index variable names. This will work:
IList<string> Quantitys = Program.RecieptList[i].ItemQuantitys.Split(new char[] {'*'});
for (int j = 0; j <= Quantitys.Count - 1; j++)
{
}
Related videos on Youtube
Nathan
Updated on August 23, 2020Comments
-
Nathan over 3 years
I'm trying to get the number of elements in this string array, but it won't let me take 1 away from
Count
.string [] Quantitys = Program.RecieptList[i].ItemQuantitys.Split(new char[] {'*'}); for (int ii = 0; i <= Quantitys.Count - 1; ii++) { }
I get an error message stating
Operator '-' cannot be applied to operands of type 'Method Group' and 'Int'.
Whats the proper way to do this?
-
Jonesopolis over 10 yearsdid you mean to use both
ii
andi
in your loop? It will loop forever unless you handlei
in your loop -
Nathan over 10 years@Jonesy Whoops :p Thanks!
-
default over 10 yearspossible duplicate of LINQ Operator '==' cannot be applied to operands of type 'method group' and 'int'
-
-
and_the_rand over 10 years@Nathan: If you're used to working with lists, the IEnumerable methods are available to you with arrays. In fact, the error you got was because you forgot parentheses for the Linq
Count()
method. -
valverij over 10 years@AustinSalonen, since
IList<>
implementsIEnumerable<>
, you could technically do the same thing with lists too. -
JSTL over 5 yearsThis was already mentioned in the comments of the first answer.
-
KDWolf over 5 years@JSTL "Comments are used to ask for clarification or to point out problems in the post. "
-
Kosmo almost 5 yearsBut
Array
implementsICollection
, which definesCount
(property). You can use it with an explicitICollection
cast (it will still returnLenght
). But why do I need to cast? -
Kosmo almost 5 yearsIs it because of explicit
ICollection.Count
interface implementation ?github.com/microsoft/referencesource/blob/… -
valverij almost 5 yearsYes,
System.Array
effectively hides theICollection.Count
, so unless you cast it asIList
(which I advise against) orICollection
, you're not hitting it directly. You could technically use it that way, but I typically steer away from usingICollection
directly outside of the occasional extension method. Need to iterate/enumerate? UseIEnumerable
. Key value pair?IDictionary
. etc, etc.