How do I search for a specific value in a generic C # list?

advertisements
class MyGenericClass<T> where T : ICompareable
{
  T[] data;

  public AddData(T[] values)
  {
     data = values;
  }
}

In my mainForm, I create 3 random numbers, and add them as values: 1 3 3, resulting in:

T[] data :  [0]1
            [1]3
            [2]3

I want to be able to search for a specific value and have the number of times that value is present in the array returned to me.

How do I do that in C#?


This should work for you:

   public class MyGenericClass<T> where T : IComparable
   {
    T[] Data;

    public void AddData(T[] values)
    {
        Data = values;

        var list = Data.ToList();
        object compareWith = new object();
        compareWith = 3;

        int count = list.Where(a=>a.CompareTo(compareWith) == 0).Count();

    }
  }