首先,本方法需要传入一个委托和一个数组
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;
}
}
}
}