Operator cannot be applied to operands of type 'Method Group' and 'int'

35,053

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++)
{

}
Share:
35,053

Related videos on Youtube

Nathan
Author by

Nathan

Updated on August 23, 2020

Comments

  • Nathan
    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?

  • and_the_rand
    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
    valverij over 10 years
    @AustinSalonen, since IList<> implements IEnumerable<>, you could technically do the same thing with lists too.
  • JSTL
    JSTL over 5 years
    This was already mentioned in the comments of the first answer.
  • KDWolf
    KDWolf over 5 years
    @JSTL "Comments are used to ask for clarification or to point out problems in the post. "
  • Kosmo
    Kosmo almost 5 years
    But Array implements ICollection, which defines Count (property). You can use it with an explicit ICollection cast (it will still return Lenght). But why do I need to cast?
  • Kosmo
    Kosmo almost 5 years
    Is it because of explicit ICollection.Count interface implementation ?github.com/microsoft/referencesource/blob/…
  • valverij
    valverij almost 5 years
    Yes, System.Array effectively hides the ICollection.Count, so unless you cast it as IList (which I advise against) or ICollection, you're not hitting it directly. You could technically use it that way, but I typically steer away from using ICollection directly outside of the occasional extension method. Need to iterate/enumerate? Use IEnumerable. Key value pair? IDictionary. etc, etc.