Created
November 28, 2016 13:00
-
-
Save romainPechot/025f5c902b4f55662efa6b5a140f2835 to your computer and use it in GitHub Desktop.
Array Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
public static class ArrayExtensions | |
{ | |
public static T GetRandom<T>(this T[] array) | |
{ | |
return array[Random.Range(0, array.Length - 1)]; | |
}// GetRandom() | |
public static bool Find<T>(this T[] array, T refValue) | |
{ | |
return array.FindFirstIndex(refValue) != -1; | |
}// Find() | |
public static int FindFirstIndex<T>(this T[] array, T refValue) | |
{ | |
for(int i = 0; i < array.Length; i++) | |
{ | |
if(EqualityComparer<T>.Default.Equals(array[i], refValue)) | |
{ | |
return i; | |
} | |
} | |
return -1; | |
}// Find() | |
}// ArrayExtensions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment