Unity 使用this关键字进行函数拓展 - Collection
【摘要】
Example:
private readonly int[] array = new int[5] { 1, 3, 5, 2, 4 }; private void Start(){ array.ForEach(m => Debug.Log(m)); array.ForEachReverse(m => Debu...
Example:
-
private readonly int[] array = new int[5] { 1, 3, 5, 2, 4 };
-
-
private void Start()
-
{
-
array.ForEach(m => Debug.Log(m));
-
array.ForEachReverse(m => Debug.Log(m));
-
array.Merge(new int[2] { 1, 2 });
-
List<int> list = array.ToList();
-
Dictionary<int, int> dic = array.ToDictionary();
-
Queue<int> queue = array.ToQueue();
-
Stack<int> stack = array.ToStack();
-
int[] newArray = array.Copy();
-
int targetIndex = array.FindIndex(m => m == 2);
-
}
Extension:
-
/// <summary>
-
/// The extension of generic collection.
-
/// </summary>
-
public static class GenericCollectionsExtension
-
{
-
#region IEnumerable
-
/// <summary>
-
/// For the each.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> self, Action<T> action)
-
{
-
foreach (var element in self)
-
{
-
action(element);
-
}
-
return self;
-
}
-
#endregion
-
-
#region Array
-
/// <summary>
-
/// For each array element.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static T[] ForEach<T>(this T[] self, Action<T> action)
-
{
-
Array.ForEach(self, action);
-
return self;
-
}
-
/// <summary>
-
/// For each array element.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static T[] ForEach<T>(this T[] self, Action<int, T> action)
-
{
-
for (int i = 0; i < self.Length; i++)
-
{
-
action(i, self[i]);
-
}
-
return self;
-
}
-
/// <summary>
-
/// For each array element reverse.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static T[] ForEachReverse<T>(this T[] self, Action<T> action)
-
{
-
for (int i = self.Length - 1; i >= 0; --i)
-
{
-
action(self[i]);
-
}
-
return self;
-
}
-
/// <summary>
-
/// For each array element reverse.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static T[] ForEachReverse<T>(this T[] self, Action<int, T> action)
-
{
-
for (int i = self.Length - 1; i >= 0; --i)
-
{
-
action(i, self[i]);
-
}
-
return self;
-
}
-
/// <summary>
-
/// Merge the two array.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="beMergedArray"></param>
-
/// <returns>The new array contains all elements of the two arrays.</returns>
-
public static T[] Merge<T>(this T[] self, T[] beMergedArray)
-
{
-
T[] retArray = new T[self.Length + beMergedArray.Length];
-
for (int i = 0; i < self.Length; i++)
-
{
-
retArray[i] = self[i];
-
}
-
for (int i = 0; i < beMergedArray.Length; i++)
-
{
-
retArray[i + self.Length] = beMergedArray[i];
-
}
-
return retArray;
-
}
-
/// <summary>
-
/// Convert the array to list.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new list converted from the array.</returns>
-
public static List<T> ToList<T>(this T[] self)
-
{
-
List<T> retList = new List<T>(self.Length);
-
for (int i = 0; i < self.Length; i++)
-
{
-
retList.Add(self[i]);
-
}
-
return retList;
-
}
-
/// <summary>
-
/// Convert the array to dictionary.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new dictionary converted from the array.</returns>
-
public static Dictionary<int,T> ToDictionary<T>(this T[] self)
-
{
-
Dictionary<int, T> retDic = new Dictionary<int, T>(self.Length);
-
for (int i = 0; i < self.Length; i++)
-
{
-
retDic[i] = self[i];
-
}
-
return retDic;
-
}
-
/// <summary>
-
/// Convert the array to queue.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new queue converted from the array.</returns>
-
public static Queue<T> ToQueue<T>(this T[] self)
-
{
-
Queue<T> retQueue = new Queue<T>();
-
for (int i = 0; i < self.Length; i++)
-
{
-
retQueue.Enqueue(self[i]);
-
}
-
return retQueue;
-
}
-
/// <summary>
-
/// Convert the array to stack.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new stack converted from the array.</returns>
-
public static Stack<T> ToStack<T>(this T[] self)
-
{
-
Stack<T> retStack = new Stack<T>();
-
for (int i = 0; i < self.Length; i++)
-
{
-
retStack.Push(self[i]);
-
}
-
return retStack;
-
}
-
/// <summary>
-
/// Copy the array.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>return an new array has the same elements.</returns>
-
public static T[] Copy<T>(this T[] self)
-
{
-
T[] retArray = new T[self.Length];
-
for (int i = 0; i < self.Length; i++)
-
{
-
retArray[i] = self[i];
-
}
-
return retArray;
-
}
-
/// <summary>
-
/// Find target.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="match"></param>
-
/// <returns></returns>
-
public static T Find<T>(this T[] self, Predicate<T> match)
-
{
-
return Array.Find(self, match);
-
}
-
/// <summary>
-
/// Find target index.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="match"></param>
-
/// <returns></returns>
-
public static int FindIndex<T>(this T[] self, Predicate<T> match)
-
{
-
return Array.FindIndex(self, match);
-
}
-
#endregion
-
-
#region List
-
/// <summary>
-
/// For each list element.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static List<T> ForEach<T>(this List<T> self, Action<T> action)
-
{
-
for (int i = 0; i < self.Count; i++)
-
{
-
action(self[i]);
-
}
-
return self;
-
}
-
/// <summary>
-
/// For each list element.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static List<T> ForEach<T>(this List<T> self, Action<int, T> action)
-
{
-
for (int i = 0; i < self.Count; i++)
-
{
-
action(i, self[i]);
-
}
-
return self;
-
}
-
/// <summary>
-
/// For each list element reverse.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static List<T> ForEachReverse<T>(this List<T> self, Action<T> action)
-
{
-
for (int i = self.Count -1 ; i >= 0; --i)
-
{
-
action(self[i]);
-
}
-
return self;
-
}
-
/// <summary>
-
/// For each list element reverse.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static List<T> ForEachReverse<T>(this List<T> self, Action<int, T> action)
-
{
-
for (int i = self.Count - 1; i >= 0; --i)
-
{
-
action(i, self[i]);
-
}
-
return self;
-
}
-
/// <summary>
-
/// Convert the list to array.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new array converted from the list.</returns>
-
public static T[] ToArray<T>(this List<T> self)
-
{
-
T[] retArray = new T[self.Count];
-
for (int i = 0; i < self.Count; i++)
-
{
-
retArray[i] = self[i];
-
}
-
return retArray;
-
}
-
/// <summary>
-
/// Convert the list to dictionary.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new dictionary converted from the list.</returns>
-
public static Dictionary<int, T> ToDictionary<T>(this List<T> self)
-
{
-
Dictionary<int, T> retDic = new Dictionary<int, T>(self.Count);
-
for (int i = 0; i < self.Count; i++)
-
{
-
retDic[i] = self[i];
-
}
-
return retDic;
-
}
-
/// <summary>
-
/// Convert the list to queue.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new queue converted from the list.</returns>
-
public static Queue<T> ToQueue<T>(this List<T> self)
-
{
-
Queue<T> retQueue = new Queue<T>();
-
for (int i = 0; i < self.Count; i++)
-
{
-
retQueue.Enqueue(self[i]);
-
}
-
return retQueue;
-
}
-
/// <summary>
-
/// Convert the list to stack.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new stack converted from the list.</returns>
-
public static Stack<T> ToStack<T>(this List<T> self)
-
{
-
Stack<T> retStack = new Stack<T>();
-
for (int i = 0; i < self.Count; i++)
-
{
-
retStack.Push(self[i]);
-
}
-
return retStack;
-
}
-
/// <summary>
-
/// Copy the list.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>return an new list has the same elements.</returns>
-
public static List<T> Copy<T>(this List<T> self)
-
{
-
List<T> retList = new List<T>(self.Count);
-
for (int i = 0; i < self.Count; i++)
-
{
-
retList[i] = self[i];
-
}
-
return retList;
-
}
-
#endregion
-
-
#region Dictionary
-
/// <summary>
-
/// For each dictionary key value paris.
-
/// </summary>
-
/// <typeparam name="K"></typeparam>
-
/// <typeparam name="V"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns></returns>
-
public static Dictionary<K,V> ForEach<K,V>(this Dictionary<K,V> self, Action<K,V> action)
-
{
-
var dicE = self.GetEnumerator();
-
while(dicE.MoveNext())
-
{
-
action(dicE.Current.Key, dicE.Current.Value);
-
}
-
dicE.Dispose();
-
return self;
-
}
-
/// <summary>
-
/// Add range for the dictionary.
-
/// </summary>
-
/// <typeparam name="K"></typeparam>
-
/// <typeparam name="V"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="beAddDic"></param>
-
/// <param name="isOverride">Override the value if have the same key.</param>
-
/// <returns></returns>
-
public static Dictionary<K,V> AddRange<K,V>(this Dictionary<K,V> self, Dictionary<K,V> beAddDic, bool isOverride = false)
-
{
-
var dicE = beAddDic.GetEnumerator();
-
while (dicE.MoveNext())
-
{
-
var current = dicE.Current;
-
if (self.ContainsKey(current.Key))
-
{
-
if (isOverride)
-
{
-
self[current.Key] = current.Value;
-
continue;
-
}
-
}
-
self.Add(current.Key, current.Value);
-
}
-
dicE.Dispose();
-
return self;
-
}
-
/// <summary>
-
/// Copy the dictionary.
-
/// </summary>
-
/// <typeparam name="K"></typeparam>
-
/// <typeparam name="V"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>return an new dictionary has the same key value pairs.</returns>
-
public static Dictionary<K,V> Copy<K,V>(this Dictionary<K,V> self)
-
{
-
Dictionary<K, V> retDic = new Dictionary<K, V>();
-
var dicE = self.GetEnumerator();
-
while (dicE.MoveNext())
-
{
-
retDic.Add(dicE.Current.Key, dicE.Current.Value);
-
}
-
dicE.Dispose();
-
return retDic;
-
}
-
#endregion
-
-
#region Queue
-
/// <summary>
-
/// For each queue element.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static Queue<T> ForEach<T>(this Queue<T> self, Action<T> action)
-
{
-
foreach (var element in self)
-
{
-
action(element);
-
}
-
return self;
-
}
-
/// <summary>
-
/// Convert the queue to array.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new array whose elements are dequeue from the queue.</returns>
-
public static T[] ToArray<T>(this Queue<T> self)
-
{
-
T[] retArray = new T[self.Count];
-
for (int i = 0; i < retArray.Length; i++)
-
{
-
retArray[i] = self.Dequeue();
-
}
-
return retArray;
-
}
-
/// <summary>
-
/// Convert the queue to list.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new list whose elements are dequeue from the queue.</returns>
-
public static List<T> ToList<T>(this Queue<T> self)
-
{
-
List<T> retList = new List<T>(self.Count);
-
for (int i = 0; i < retList.Count; i++)
-
{
-
retList.Add(self.Dequeue());
-
}
-
return retList;
-
}
-
/// <summary>
-
/// Convert the queue to stack.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new stack whose elements are dequeue from the queue.</returns>
-
public static Stack<T> ToStack<T>(this Queue<T> self)
-
{
-
Stack<T> retStack = new Stack<T>();
-
int count = self.Count;
-
for (int i = 0; i < count; i++)
-
{
-
retStack.Push(self.Dequeue());
-
}
-
return retStack;
-
}
-
/// <summary>
-
/// Convert the queue to dictionary.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new dictionary whose values are dequeue from the queue.</returns>
-
public static Dictionary<int, T> ToDictionary<T>(this Queue<T> self)
-
{
-
Dictionary<int, T> retDic = new Dictionary<int, T>(self.Count);
-
for (int i = 0; i < retDic.Count; i++)
-
{
-
retDic[i] = self.Dequeue();
-
}
-
return retDic;
-
}
-
/// <summary>
-
/// Copy the queue.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>return an new queue has the same elements.</returns>
-
public static Queue<T> Copy<T>(this Queue<T> self)
-
{
-
Queue<T> retQueue = new Queue<T>();
-
foreach (var element in self)
-
{
-
retQueue.Enqueue(element);
-
}
-
return retQueue;
-
}
-
#endregion
-
-
#region Stack
-
/// <summary>
-
/// For each stack element.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <param name="action"></param>
-
/// <returns>self</returns>
-
public static Stack<T> ForEach<T>(this Stack<T> self, Action<T> action)
-
{
-
foreach (var element in self)
-
{
-
action(element);
-
}
-
return self;
-
}
-
/// <summary>
-
/// Convert the stack to array.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new array whose elements are pop from the stack.</returns>
-
public static T[] ToArray<T>(this Stack<T> self)
-
{
-
T[] retArray = new T[self.Count];
-
for (int i = 0; i < retArray.Length; i++)
-
{
-
retArray[i] = self.Pop();
-
}
-
return retArray;
-
}
-
/// <summary>
-
/// Convert the stack to list.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new list whose elements are pop from the stack.</returns>
-
public static List<T> ToList<T>(this Stack<T> self)
-
{
-
List<T> retList = new List<T>(self.Count);
-
for (int i = 0; i < retList.Count; i++)
-
{
-
retList.Add(self.Pop());
-
}
-
return retList;
-
}
-
/// <summary>
-
/// Convert the stack to queue.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new queue whose elements are pop from the stack.</returns>
-
public static Queue<T> ToQueue<T>(this Stack<T> self)
-
{
-
Queue<T> retQueue = new Queue<T>();
-
int count = self.Count;
-
for (int i = 0; i < count; i++)
-
{
-
retQueue.Enqueue(self.Pop());
-
}
-
return retQueue;
-
}
-
/// <summary>
-
/// Convert the stack to dictionary.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>The new dictionary whose values are pop from the stack.</returns>
-
public static Dictionary<int, T> ToDictionary<T>(this Stack<T> self)
-
{
-
Dictionary<int, T> retDic = new Dictionary<int, T>(self.Count);
-
for (int i = 0; i < retDic.Count; i++)
-
{
-
retDic[i] = self.Pop();
-
}
-
return retDic;
-
}
-
/// <summary>
-
/// Copy the stack.
-
/// </summary>
-
/// <typeparam name="T"></typeparam>
-
/// <param name="self"></param>
-
/// <returns>return an new stack has the same elements.</returns>
-
public static Stack<T> Copy<T>(this Stack<T> self)
-
{
-
Stack<T> retStack = new Stack<T>();
-
foreach (var element in self)
-
{
-
retStack.Push(element);
-
}
-
return retStack;
-
}
-
#endregion
-
}
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/109056361
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)