自己写的一个对于数组的操作方法

首先,本方法需要传入一个委托和一个数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication19
{
    /// 
    /// 可以生成一个动态链接库
    /// 传入一个int数组和一个委托方法
    /// 
    class Class1
    {
            public delegate bool InterFilter(int i);
        /// 
        /// 传入int数组和一个委托方法,返回一个符合条件的数组
        /// 
        /// 
        /// 
        /// 
  
  
            public static int[] FilterArrayOfInt(int[] ints, InterFilter filter)
            {
                ArrayList alist = new ArrayList();
                foreach (int i in ints)
                {
                    if (filter(i))
                    {
                        alist.Add(i);
                    }
                }
                return (int[])alist.ToArray(typeof(int));
            }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication19
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ints = { 1,2,34,45,23,3};
            List
  
  
   
    mylist = new List
   
   
    
    ();

            //int[] num = Class1.FilterArrayOfInt(ints, delegate(int i) { return (i > 10); });
            int[] num = Class1.FilterArrayOfInt(ints, i => (i % 2 == 0));
            //int[] num=ints.Select(i=>(i>10))
            for (int i = 0; i < num.Length; i++)
            {
                Console.WriteLine(num[i].ToString());
            }

            //for (int i = 0; i < Class1.FilterArrayOfInt(ints, mytest).Length; i++)
            //{
            //    mylist.Add(Class1.FilterArrayOfInt(ints, mytest)[i]);
            //}
            //for (int j = 0; j < mylist.Count; j++)
            //{
            //    Console.WriteLine(mylist[j]);
            //}  
        }
        /// 
    
    
        /// 传入的委托方法(注意和定义的方法类型一致static)
        /// 
        /// 
    
    
        /// 
    
    
        private static bool mytest(int i)
        {
            if (i > 10)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}


   
   
  
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值