您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 3浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity 使用this关键字进行函数拓展 - Collection

CoderZ1010 发布时间:2020-10-13 17:29:43 ,浏览量:3

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 list = array.ToList();
    Dictionary dic = array.ToDictionary();
    Queue queue = array.ToQueue();
    Stack stack = array.ToStack();
    int[] newArray = array.Copy();
    int targetIndex = array.FindIndex(m => m == 2);
}

Extension:

    /// 
    /// The extension of generic collection.
    /// 
    public static class GenericCollectionsExtension
    {
        #region IEnumerable
        /// 
        /// For the each.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static IEnumerable ForEach(this IEnumerable self, Action action)
        {
            foreach (var element in self)
            {
                action(element);
            }
            return self;
        }
        #endregion

        #region Array
        /// 
        /// For each array element.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static T[] ForEach(this T[] self, Action action)
        {
            Array.ForEach(self, action);
            return self;
        }
        /// 
        /// For each array element.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static T[] ForEach(this T[] self, Action action)
        {
            for (int i = 0; i < self.Length; i++)
            {
                action(i, self[i]);
            }
            return self;
        }
        /// 
        /// For each array element reverse.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static T[] ForEachReverse(this T[] self, Action action)
        {
            for (int i = self.Length - 1; i >= 0; --i)
            {
                action(self[i]);
            }
            return self;
        }
        /// 
        /// For each array element reverse.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static T[] ForEachReverse(this T[] self, Action action)
        {
            for (int i = self.Length - 1; i >= 0; --i)
            {
                action(i, self[i]);
            }
            return self;
        }
        /// 
        /// Merge the two array.
        /// 
        /// 
        /// 
        /// 
        /// The new array contains all elements of the two arrays.
        public static T[] Merge(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;
        }
        /// 
        /// Convert the array to list.
        /// 
        /// 
        /// 
        /// The new list converted from the array.
        public static List ToList(this T[] self)
        {
            List retList = new List(self.Length);
            for (int i = 0; i < self.Length; i++)
            {
                retList.Add(self[i]);
            }
            return retList;
        }
        /// 
        /// Convert the array to dictionary.
        /// 
        /// 
        /// 
        /// The new dictionary converted from the array.
        public static Dictionary ToDictionary(this T[] self)
        {
            Dictionary retDic = new Dictionary(self.Length);
            for (int i = 0; i < self.Length; i++)
            {
                retDic[i] = self[i];
            }
            return retDic;
        }
        /// 
        /// Convert the array to queue.
        /// 
        /// 
        /// 
        /// The new queue converted from the array.
        public static Queue ToQueue(this T[] self)
        {
            Queue retQueue = new Queue();
            for (int i = 0; i < self.Length; i++)
            {
                retQueue.Enqueue(self[i]);
            }
            return retQueue;
        }   
        /// 
        /// Convert the array to stack.
        /// 
        /// 
        /// 
        /// The new stack converted from the array.
        public static Stack ToStack(this T[] self)
        {
            Stack retStack = new Stack();
            for (int i = 0; i < self.Length; i++)
            {
                retStack.Push(self[i]);
            }
            return retStack;
        }
        /// 
        /// Copy the array.
        /// 
        /// 
        /// 
        /// return an new array has the same elements.
        public static T[] Copy(this T[] self)
        {
            T[] retArray = new T[self.Length];
            for (int i = 0; i < self.Length; i++)
            {
                retArray[i] = self[i];
            }
            return retArray;
        }
        /// 
        /// Find target.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Find(this T[] self, Predicate match)
        {
            return Array.Find(self, match);
        }
        /// 
        /// Find target index.
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int FindIndex(this T[] self, Predicate match)
        {
            return Array.FindIndex(self, match);
        }
        #endregion

        #region List
        /// 
        /// For each list element.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static List ForEach(this List self, Action action)
        {
            for (int i = 0; i < self.Count; i++)
            {
                action(self[i]);
            }
            return self;
        }
        /// 
        /// For each list element.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static List ForEach(this List self, Action action)
        {
            for (int i = 0; i < self.Count; i++)
            {
                action(i, self[i]);
            }
            return self;
        }
        /// 
        /// For each list element reverse.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static List ForEachReverse(this List self, Action action)
        {
            for (int i = self.Count -1 ; i >= 0; --i)
            {
                action(self[i]);
            }
            return self;
        }
        /// 
        /// For each list element reverse.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static List ForEachReverse(this List self, Action action)
        {
            for (int i = self.Count - 1; i >= 0; --i)
            {
                action(i, self[i]);
            }
            return self;
        }
        /// 
        /// Convert the list to array.
        /// 
        /// 
        /// 
        /// The new array converted from the list.
        public static T[] ToArray(this List self)
        {
            T[] retArray = new T[self.Count];
            for (int i = 0; i < self.Count; i++)
            {
                retArray[i] = self[i];
            }
            return retArray;
        }
        /// 
        /// Convert the list to dictionary.
        /// 
        /// 
        /// 
        /// The new dictionary converted from the list.
        public static Dictionary ToDictionary(this List self)
        {
            Dictionary retDic = new Dictionary(self.Count);
            for (int i = 0; i < self.Count; i++)
            {
                retDic[i] = self[i];
            }
            return retDic;
        }
        /// 
        /// Convert the list to queue.
        /// 
        /// 
        /// 
        /// The new queue converted from the list.
        public static Queue ToQueue(this List self)
        {
            Queue retQueue = new Queue();
            for (int i = 0; i < self.Count; i++)
            {
                retQueue.Enqueue(self[i]);
            }
            return retQueue;
        }
        /// 
        /// Convert the list to stack.
        /// 
        /// 
        /// 
        /// The new stack converted from the list.
        public static Stack ToStack(this List self)
        {
            Stack retStack = new Stack();
            for (int i = 0; i < self.Count; i++)
            {
                retStack.Push(self[i]);
            }
            return retStack;
        }
        /// 
        /// Copy the list.
        /// 
        /// 
        /// 
        /// return an new list has the same elements.
        public static List Copy(this List self)
        {
            List retList = new List(self.Count);
            for (int i = 0; i < self.Count; i++)
            {
                retList[i] = self[i];
            }
            return retList;
        }
        #endregion

        #region Dictionary
        /// 
        /// For each dictionary key value paris.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static Dictionary ForEach(this Dictionary self, Action action)
        {
            var dicE = self.GetEnumerator();
            while(dicE.MoveNext())
            {
                action(dicE.Current.Key, dicE.Current.Value);
            }
            dicE.Dispose();
            return self;
        }
        /// 
        /// Add range for the dictionary.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// Override the value if have the same key.
        /// 
        public static Dictionary AddRange(this Dictionary self, Dictionary 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;
        }
        /// 
        /// Copy the dictionary.
        /// 
        /// 
        /// 
        /// 
        /// return an new dictionary has the same key value pairs.
        public static Dictionary Copy(this Dictionary self)
        {
            Dictionary retDic = new Dictionary();
            var dicE = self.GetEnumerator();
            while (dicE.MoveNext())
            {
                retDic.Add(dicE.Current.Key, dicE.Current.Value);
            }
            dicE.Dispose();
            return retDic;
        }
        #endregion

        #region Queue
        /// 
        /// For each queue element.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static Queue ForEach(this Queue self, Action action)
        {
            foreach (var element in self)
            {
                action(element);
            }
            return self;
        }
        /// 
        /// Convert the queue to array.
        /// 
        /// 
        /// 
        /// The new array whose elements are dequeue from the queue.
        public static T[] ToArray(this Queue self)
        {
            T[] retArray = new T[self.Count];
            for (int i = 0; i < retArray.Length; i++)
            {
                retArray[i] = self.Dequeue();
            }
            return retArray;
        }
        /// 
        /// Convert the queue to list.
        /// 
        /// 
        /// 
        /// The new list whose elements are dequeue from the queue.
        public static List ToList(this Queue self)
        {
            List retList = new List(self.Count);
            for (int i = 0; i < retList.Count; i++)
            {
                retList.Add(self.Dequeue());
            }
            return retList;
        }
        /// 
        /// Convert the queue to stack.
        /// 
        /// 
        /// 
        /// The new stack whose elements are dequeue from the queue.
        public static Stack ToStack(this Queue self)
        {
            Stack retStack = new Stack();
            int count = self.Count;
            for (int i = 0; i < count; i++)
            {
                retStack.Push(self.Dequeue());
            }
            return retStack;
        }
        /// 
        /// Convert the queue to dictionary.
        /// 
        /// 
        /// 
        /// The new dictionary whose values are dequeue from the queue.
        public static Dictionary ToDictionary(this Queue self)
        {
            Dictionary retDic = new Dictionary(self.Count);
            for (int i = 0; i < retDic.Count; i++)
            {
                retDic[i] = self.Dequeue();
            }
            return retDic;
        }
        /// 
        /// Copy the queue.
        /// 
        /// 
        /// 
        /// return an new queue has the same elements.
        public static Queue Copy(this Queue self)
        {
            Queue retQueue = new Queue();
            foreach (var element in self)
            {
                retQueue.Enqueue(element);
            }
            return retQueue;
        }
        #endregion

        #region Stack
        /// 
        /// For each stack element.
        /// 
        /// 
        /// 
        /// 
        /// self
        public static Stack ForEach(this Stack self, Action action)
        {
            foreach (var element in self)
            {
                action(element);
            }
            return self;
        }
        /// 
        /// Convert the stack to array.
        /// 
        /// 
        /// 
        /// The new array whose elements are pop from the stack.
        public static T[] ToArray(this Stack self)
        {
            T[] retArray = new T[self.Count];
            for (int i = 0; i < retArray.Length; i++)
            {
                retArray[i] = self.Pop();
            }
            return retArray;
        }
        /// 
        /// Convert the stack to list.
        /// 
        /// 
        /// 
        /// The new list whose elements are pop from the stack.
        public static List ToList(this Stack self)
        {
            List retList = new List(self.Count);
            for (int i = 0; i < retList.Count; i++)
            {
                retList.Add(self.Pop());
            }
            return retList;
        }
        /// 
        /// Convert the stack to queue.
        /// 
        /// 
        /// 
        /// The new queue whose elements are pop from the stack.
        public static Queue ToQueue(this Stack self)
        {
            Queue retQueue = new Queue();
            int count = self.Count;
            for (int i = 0; i < count; i++)
            {
                retQueue.Enqueue(self.Pop());
            }
            return retQueue;
        }
        /// 
        /// Convert the stack to dictionary.
        /// 
        /// 
        /// 
        /// The new dictionary whose values are pop from the stack.
        public static Dictionary ToDictionary(this Stack self)
        {
            Dictionary retDic = new Dictionary(self.Count);
            for (int i = 0; i < retDic.Count; i++)
            {
                retDic[i] = self.Pop();
            }
            return retDic;
        }
        /// 
        /// Copy the stack.
        /// 
        /// 
        /// 
        /// return an new stack has the same elements.
        public static Stack Copy(this Stack self)
        {
            Stack retStack = new Stack();
            foreach (var element in self)
            {
                retStack.Push(element);
            }
            return retStack;
        }
        #endregion
    }
关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.0711s