2

If I have an ArrayList called holder: {2,3,3,5,4,7,1,7,8,4}. Let say I wanted to find the count of certain element's occurrence in the above array within a certain range, I have these function:

In VB.Net:

Private Function getValInRange(ByVal lowerVal As Integer, ByVal upperVal As Integer, ByVal holder As ArrayList) As Integer
        Dim count As Integer = 0
        For Each item As Integer In holder
            If (item <= upperVal AndAlso item >= lowerVal) Then
                count = count + 1
            End If
        Next
        Return count
End Function

In C#:

private int getValInRange(int lowerVal, int upperVal, ArrayList holder)
{
    int count = 0;
    foreach (int item in holder)
    {
        if ((item <= upperVal && item >= lowerVal))
        {
            count = count + 1;
        }
    }

    return count;
}

So, when I query count = getValInRange(3,5,holder), I shall get a return of 5

I know the above function will be able to satisfy my needs, but I wonder if there is already a built in function that I can use. I plan to clean up my code and learn at the same time. Thanks a lot...

4
  • 2
    Do you have to use the non-generic ArrayList? That's been somewhat-obsolete since 2005. If you could use some implementation of IEnumerable<int> (e.g. List<int> or int[]) it would be much simpler. I'd also suggest following .NET naming conventions - I'd call the method CountValuesInRange for example. Commented Jan 25, 2018 at 7:38
  • @Jon Skeet I am using ArrayList all the time. But you suggestion to use different implementation is good. Maybe shall start to use that from my next attempt onwards. Anyway, I prefer to not to have a separate method CountValuesInRange Commented Jan 25, 2018 at 7:54
  • 1
    "I am using ArrayList all the time" - I'd stop doing that if I were you. I wouldn't wait, I'd learn about generic collections right now. Commented Jan 25, 2018 at 7:55
  • @JonSkeet thanks for your suggestion man. I learned something new Commented Jan 25, 2018 at 7:56

3 Answers 3

4

I don't think there is such a built-in method in .NET Core, but you can write this more concisely, using Enumerable.Where.

 holder.Cast<int>().Where(x => x <= upperLimit && x >= lowerLimit).Count()

ArrayList is actually obsolete. If you don't have to stick with it, try changing to a List<int>. That way, you don't need the call to Cast:

 holder.Where(x => x <= upperLimit && x >= lowerLimit).Count()

Alternatively,

 holder.Count(x => x <= upperLimit && x >= lowerLimit)

The x => ... thing, if you didn't know, is called a lambda expression, learn more about them here.

There are lots of other cool methods that help you with dealing with IEnumerable<T> in the System.Linq namespace! This cool collection of helper methods is called Language INtegrated Query (LINQ).

Sign up to request clarification or add additional context in comments.

2 Comments

You can write it shorter if you put lambda inside Count and remove Where
OP also asked for VB, so holder.Sum(Function(x) If(x >= lowerVal AndAlso x <= upperVal, 1, 0)) as .Count in VB 15.0 (2017) doesn't accept a lambda. Or just the .Where....Count version.
4

If you use List<int>, or any other IEnumerable<int>, Linq has the Count extension

private int getValInRange(int lowerVal, int upperVal, List<int> holder)
{
    return holder.Count(item => item <= upperVal && item >= lowerVal);
}

Edit: As @MarcGravell suggested, you can also cast the ArrayList to IEnumerable

private int getValInRange(int lowerVal, int upperVal, ArrayList holder)
{
    return holder.Cast<int>().Count(item => item <= upperVal && item >= lowerVal);
}

1 Comment

Note you can still use this with ArrayList, but you'd need to use .Cast<int>().Count(...) - although I agree that moving away from ArrayList is hugely desirable
1

I would suggest to use LINQ and change that ArrayList to IEnumerable, however using IEnumerable<int> is more preferred.

private int GetValInRange(int lowerVal, int upperVal, IEnumerable holder)
    {
        return holder.Cast<int>().Count(item => item <= upperVal && item >= lowerVal);
    }

1 Comment

Unfortunately using ArrayList currently

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.