using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static 泛型.Program;
namespace 泛型
{
class Program
{
public delegate int Arrdelegate<T>(T[] a, T b);
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 5, 5 };
string[] b = { "a", "a", "a", "b", "b" };
//泛型委托**********************
//Arrdelegate<int> arrdelegate = Arr;
//int result = arrdelegate(a, 2);
//Console.WriteLine(result);
//func<>委托********************
//Func<int[], int, int> arrdelegate = Arr;
//int result = arrdelegate(a, 5);
//Console.WriteLine(result);
//匿名函数***********************
//Arrdelegate<int> arrdelegate = delegate (int[] arr, int c)
//{
// int count = 0;
// for (int i = 0; i < arr.Length; i++)
// {
// if (arr[i].Equals(c))
// {
// count++;
// }
// }
// return count;
//};
//int result = arrdelegate(a, 5);
//Console.WriteLine(result);
//lambda表达式*********************
Func<int[], int, int> arrdelegate = (int[] arr, int c) =>
{
int count = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i].Equals(c))
{
count++;
}
}
return count;
};
int result = arrdelegate(a, 3);
//int result = arrdelegate.Invoke(a, 5); 也可通过Invoke来激发委托
Console.WriteLine(result);
Console.ReadKey();
}
public static int Arr<T>(T[] a, T b)
{
int count = 0;
for (int i = 0; i < a.Length; i++)
{
if (a[i].Equals(b))
{
count++;
}
}
return count;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static 泛型.Program;
namespace 泛型
{
class Program
{
public delegate int Arrdelegate<T>(T[] a, T b);
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5, 5, 5 };
string[] b = { "a", "a", "a", "b", "b" };
//泛型委托**********************
//Arrdelegate<int> arrdelegate = Arr;
//int result = arrdelegate(a, 2);
//Console.WriteLine(result);
//func<>委托********************
//Func<int[], int, int> arrdelegate = Arr;
//int result = arrdelegate(a, 5);
//Console.WriteLine(result);
//匿名函数***********************
//Arrdelegate<int> arrdelegate = delegate (int[] arr, int c)
//{
// int count = 0;
// for (int i = 0; i < arr.Length; i++)
// {
// if (arr[i].Equals(c))
// {
// count++;
// }
// }
// return count;
//};
//int result = arrdelegate(a, 5);
//Console.WriteLine(result);
//lambda表达式*********************
Func<int[], int, int> arrdelegate = (int[] arr, int c) =>
{
int count = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i].Equals(c))
{
count++;
}
}
return count;
};
int result = arrdelegate(a, 3);
//int result = arrdelegate.Invoke(a, 5); 也可通过Invoke来激发委托
Console.WriteLine(result);
Console.ReadKey();
}
public static int Arr<T>(T[] a, T b)
{
int count = 0;
for (int i = 0; i < a.Length; i++)
{
if (a[i].Equals(b))
{
count++;
}
}
return count;
}
}
}