Robin Rodricks
Robin Rodricks

Reputation: 113956

Generic functions on Arrays in .NET

I often work with arrays of int/float and other things on a regular basis in C# since they are faster (maybe?) than List. Currently I've got dozens of functions implemented per type:

/// <summary>
/// Checks if the array contains the given value.
/// </summary>
public static bool contains(int[] values, int value) {
    for (int s = 0, sl = values.Length; s < sl; s++) {
        if (values[s] == value) {
            return true;
        }
    }
    return false;
}
/// <summary>
/// Checks if the array contains the given value.
/// </summary>
public static bool contains(float[] values, float value) {
    for (int s = 0, sl = values.Length; s < sl; s++) {
        if (values[s] == value) {
            return true;
        }
    }
    return false;
}

Is there a way to implement these in a generic way? Or is that only possible if I use List<T>?

Upvotes: 1

Views: 637

Answers (2)

Kek
Kek

Reputation: 3195

Thanks @Servy for your corrections :

/// <summary>
/// Checks if the array contains the given value.
/// </summary>
public static bool Contains<T>(T[] values, T value) {
    for (int s = 0, sl = values.Length; s < sl; s++) {
        if (object.Equals(values[s].value)) {
            return true;
        }
    }
    return false;
}

For this kind of things, you may also use Linq :

using System.Linq;
...
var myArray = new float[12];
...
if(myArray.Any(a => a == 2.4))

Upvotes: 2

x0n
x0n

Reputation: 52420

The System.Array type has a dizzying array of static methods that can help you search, arrange, sort etc your arrays. You definitely do not need to roll your own. These static methods (like Find, FindAll) also accept generic arguments. Here's the list, dumped with the aid of PowerShell:

Name                           Definition                    
----                           ----------                    
AsReadOnly                     static System.Collections.... 
BinarySearch                   static int BinarySearch(ar... 
Clear                          static void Clear(array ar... 
ConstrainedCopy                static void ConstrainedCop... 
ConvertAll                     static TOutput[] ConvertAl... 
Copy                           static void Copy(array sou... 
CreateInstance                 static array CreateInstanc... 
Equals                         static bool Equals(System.... 
Exists                         static bool Exists[T](T[] ... 
Find                           static T Find[T](T[] array... 
FindAll                        static T[] FindAll[T](T[] ... 
FindIndex                      static int FindIndex[T](T[... 
FindLast                       static T FindLast[T](T[] a... 
FindLastIndex                  static int FindLastIndex[T... 
ForEach                        static void ForEach[T](T[]... 
IndexOf                        static int IndexOf(array a... 
LastIndexOf                    static int LastIndexOf(arr... 
ReferenceEquals                static bool ReferenceEqual... 
Resize                         static void Resize[T]([ref... 
Reverse                        static void Reverse(array ... 
Sort                           static void Sort(array arr... 
TrueForAll                     static bool TrueForAll[T](... 

Upvotes: 3

Related Questions