0

I have the following class:

public class Words
{
    public string word;
    public bool correct;
}

and I have an array of Words.

Words[] words;

I wish to count how many elements in my array have the property correct set to true.

What would be the most efficient way to do this?

3
  • 3
    Most efficient would be keep track of how elements have the property correct set to true, so you don't have to count them. Commented Apr 30, 2013 at 12:58
  • Depends @dtb, that isn't always the best options. Commented Apr 30, 2013 at 13:54
  • I agree with KingCronus. Plus it would require storing an extra variable, which would only make the code more messy. I believe that the LINQ solution is more elegant and definitely answers my question :) Commented May 2, 2013 at 7:42

1 Answer 1

6

Using LINQ

words.Where(w=>w.correct);

That should do the trick to filter.


But if you only want to count:

words.Count(w=>w.correct);
Sign up to request clarification or add additional context in comments.

1 Comment

I knew it was something like that, I just didn't know the correct syntax/method :) Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.