MyList 泛型委托

 

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++;
        }
    }
}

 

转载于:https://www.cnblogs.com/wlzhang/p/7677754.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值