Created
May 20, 2014 10:52
-
-
Save v21/152304f32e8d05bec504 to your computer and use it in GitHub Desktop.
List extensions in c#
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.Linq; | |
using UnityEngine; | |
using System; | |
using System.Collections.Generic; | |
using Debug = UnityEngine.Debug; | |
using Object = UnityEngine.Object; | |
using Random = UnityEngine.Random; | |
public static class Extensions | |
{ | |
public static T PopFirst<T>(this IList<T> t) | |
{ | |
T element = t[0]; | |
t.RemoveAt(0); | |
return element; | |
} | |
public static void PushFirst<T>(this IList<T> t, T element) | |
{ | |
t.Insert(0, element); | |
} | |
public static T PopLast<T>(this IList<T> t) | |
{ | |
T element = t[t.Count - 1]; | |
t.RemoveAt(t.Count - 1); | |
return element; | |
} | |
public static void PushLast<T>(this IList<T> t, T element) | |
{ | |
t.Add(element); | |
} | |
public static T PickRandom<T>(this IEnumerable<T> ie) | |
{ | |
List<T> t = ie as List<T> ?? ie.ToList(); | |
if(t.Count == 0) | |
{ | |
Debug.LogError("Range is zero!"); | |
return default(T); | |
} | |
return t[Random.Range(0, t.Count)]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment