using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 委托Test
{
delegate bool FilterDelegate(int i);
class Program
{
static void Main(string[] args)
{
int[] array = { 1, 2, 3, 5, 6, 6, 7, 8, 9 };
List<int> newList = MyFilter(array,FilterOdd);
foreach (int item in newList)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
static List<int> MyFilter(int[] array, FilterDelegate filter)
{
List<int> list = new List<int>();
for (int i = 0; i < array.Length; i++)
{
if (filter(i))
{
list.Add(i);
}
}
return list;
}
/// <summary>
/// 偶数
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
static bool FilterEven(int i)
{
return i % 2 == 0;
}
/// <summary>
/// 奇数
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
static bool FilterOdd(int i)
{
return i % 2 == 1;
}
}
}
//例如
delegate void Del(int x);
....
Del d = delegate(int k) { /* ... */ };
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 委托Test
{
delegate bool FilterDelegate(int i);
class Program
{
static void Main(string[] args)
{
int[] array = { 1, 2, 3, 5, 6, 6, 7, 8, 9 };
//使用匿名方法来求偶数
List<int> newList = MyFilter(array, delegate(int i) {
return i % 2 == 0;
});
foreach (int item in newList)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
static List<int> MyFilter(int[] array, FilterDelegate filter)
{
List<int> list = new List<int>();
for (int i = 0; i < array.Length; i++)
{
if (filter(i))
{
list.Add(i);
}
}
return list;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 委托Test
{
delegate bool FilterDelegate(int i);
class Program
{
static void Main(string[] args)
{
int[] array = { 1, 2, 3, 5, 6, 6, 7, 8, 9 };
//使用Lambda表达式来求偶数
List<int> newList = MyFilter(array, i => i % 2==0);
foreach (int item in newList)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
static List<int> MyFilter(int[] array, FilterDelegate filter)
{
List<int> list = new List<int>();
for (int i = 0; i < array.Length; i++)
{
if (filter(i))
{
list.Add(i);
}
}
return list;
}
}
}