码迷,mamicode.com
首页 > 其他好文 > 详细

MyList 泛型委托

时间:2017-10-16 18:07:56      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:ica   col   cep   creat   except   style   logs   cti   temp   

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Tool
{
    ///where T : class  使用的泛型只能是类 ,删除这句话,可以是任何类型
    public class MyList<T> : IEnumerable where T : class
    {
        public T this[int index] { get { return Ts[index]; } }
        public int Count = 0;
        private T[] Ts = new T[4];
        public int Add(T t)
        {
            if (Count == Ts.Length)
            {
                T[] Tstemp = new T[Ts.Length * 2];
                for (int i = 0; i < Ts.Length; i++)
                {
                    Tstemp[i] = Ts[i];
                }
                Ts = Tstemp;
            }
            Ts[Count] = t;
            Count++;
            return Count;
        }


        //public IEnumerator GetEnumerator()
        //{
        //    return Ts.GetEnumerator();
        //}
        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return Ts[i];
            }
        }
        public delegate TResult MyFunc<in T, out TResult>(T arg);
        public IEnumerable<T>
            Where(MyFunc<T, bool> predicate)
        {
            for (int i = 0; i < Count; i++)
            {
                if (!predicate.Invoke(Ts[i]))
                {
                    continue;
                }
                yield return Ts[i];
            }
        }
        public T Find(MyFunc<T, bool> predicate)
        {

            for (int i = 0; i < Count; i++)
            {
                if (predicate.Invoke(Ts[i]))
                {
                    return Ts[i];
                }

            }
            return Activator.CreateInstance<T>();

        }
        public bool RomveAt(int index)
        {
            try
            {
                for (int i = index; i <= Count - 1; i++)
                {
                    Ts[i] = Ts[i + 1];
                }
                Count--;
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void Insert(int index, T item)
        {
            if (Ts.Length == Count)
            {
                T[] Tstemp = new T[Ts.Length * 2];
                for (int i = 0; i < Ts.Length; i++)
                {
                    Tstemp[i] = Ts[i];
                }
                Ts = Tstemp;
            }
            T temp = Ts[index];
            for (int i = Count; i >= index; i--)
            {
                Ts[i + 1] = Ts[i];
            }
            Ts[index] = item;
            Count++;
        }
    }
}

 

MyList 泛型委托

标签:ica   col   cep   creat   except   style   logs   cti   temp   

原文地址:http://www.cnblogs.com/wlzhang/p/7677754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!