-
Notifications
You must be signed in to change notification settings - Fork 16
C.Collection
Support method for collections
bool IsNullOrEmpty<T>(this List<T>)
bool IsNullOrEmpty<T>(this T[])
bool IsNullOrEmpty<TKey, TValue>(this Dictionary<TKey, TValue>)
Check null or empty for list, array and dictionary
Example
private List<int> _list;
var flag = _list.IsNullOrEmpty();
void Shuffle<T>(this T[])
void Shuffle<T>(this List<T>)
IDictionary<T1, T2> Shuffle<T1, T2>(this IDictionary<T1, T2>)
Shuffle element in collections, For arrays and lists, it will be changed directly, while dictionary will return a new shuffled dictionary and not change to the original dictionary
Example
private List<int> _list = new {1, 2, 3, 4, 5};
_list.Shuffle();
IDictionary<TKey, TValue> MakeDictionary<TKey, TValue>(this TKey[] keys, TValue[] values)
IDictionary<TKey, TValue> MakeDictionary<TKey, TValue>(this IList<TKey> keys, IList<TValue> values)
make dictionay from list key and list value
Example
private List<int> keys = new {1, 2, 3, 4, 5};
private List<string> values = new {"a", "b", "c", "d", "e"};
keys.MakeDictionary(values);
T PickRandom<T>(this T[])
T PickRandom<T>(this List<T>)
Indicates the random value in the collections. If collections is empty return default vaule of T.
Return a random value of index within [0..maxExclusive) (Read Only)
maxExclusive = length of collections
maxExclusive is exclusive, so for example collections has 10 element will return a value of index between 0 and 9, each with approximately equal probability
If maxExclusive equal 0, value of index 0 will be returned.
Example
private List<int> keys = new {1, 2, 3, 4, 5};
var key = keys.PickRandom();
(T, int) PickRandomWithIndex<T>(this T[])
(T, int) PickRandomWithIndex<T>(this List<T>)
Indicates the random value in the collections and index of value inside collection. If collections is empty return default vaule of T.
Return a random value of index within [0..maxExclusive) (Read Only)
maxExclusive = length of collections
maxExclusive is exclusive, so for example collections has 10 element will return a value of index between 0 and 9, each with approximately equal probability
If maxExclusive equal 0, value of index 0 will be returned.
Example
private List<int> keys = new {1, 2, 3, 4, 5};
(int key, int index) = keys.PickRandomWithIndex();
T PopRandom<T>(this List<T>)
Indicates the random value in the collections and also remove that element. If collections is empty return default vaule of T.
Return a random value of index within [0..maxExclusive) (Read Only)
maxExclusive = length of collections
maxExclusive is exclusive, so for example collections has 10 element will return a value of index between 0 and 9, each with approximately equal probability
If maxExclusive equal 0, value of index 0 will be returned.
Example
private List<int> keys = new {1, 2, 3, 4, 5};
var key = keys.PopRandom(); // keys will be only 4 elements left
(T, int) PopRandomWithIndex<T>(this List<T>)
Indicates the random value in the collections and return index of element and also remove that element. If collections is empty return default vaule of T.
Return a random value of index within [0..maxExclusive) (Read Only)
maxExclusive = length of collections
maxExclusive is exclusive, so for example collections has 10 element will return a value of index between 0 and 9, each with approximately equal probability
If maxExclusive equal 0, value of index 0 will be returned.
Example
private List<int> keys = new {1, 2, 3, 4, 5};
(int key, int index) = keys.PopRandomWithIndex(); // keys will be only 4 elements left
void Swap<T>(this T[], int, int)
void Swap<T>(this List<T>, int, int)
swap element in array
Example
private List<int> keys = new {1, 2, 3, 4, 5};
keys.Swap(0, 1); // keys now look like {2, 1, 3, 4, 5}