cs ConsoleApp
class Program
{
static int[] arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static void Main(string[] args)
{
var result1= arr.Where ( new Func<int,bool>(Filter));//用Func委托和Filter方法
var result2 = arr.Where(delegate(int value)
{ return value % 2 == 0; });//用匿名委托
var result3 = arr.Where(value => value % 2 == 0);//用lambda表达式
var result4 = arr.Where(value =>Filter(value));//用lambda表达式+具名方法
var result5 = arr.Where(value => {
return Filter(value);
});//lambda表达式+匿名方法
}
static bool Filter(int value)
{
return value % 2 == 0;
}
static void Write(IEnumerable<int> arr)
{
foreach (var ele in arr)
Console.WriteLine(ele);
}
}
{
static int[] arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static void Main(string[] args)
{
var result1= arr.Where ( new Func<int,bool>(Filter));//用Func委托和Filter方法
var result2 = arr.Where(delegate(int value)
{ return value % 2 == 0; });//用匿名委托
var result3 = arr.Where(value => value % 2 == 0);//用lambda表达式
var result4 = arr.Where(value =>Filter(value));//用lambda表达式+具名方法
var result5 = arr.Where(value => {
return Filter(value);
});//lambda表达式+匿名方法
}
static bool Filter(int value)
{
return value % 2 == 0;
}
static void Write(IEnumerable<int> arr)
{
foreach (var ele in arr)
Console.WriteLine(ele);
}
}
vb ConsoleApp
Module Module1
Dim arr As Integer() = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Sub Main()
Dim result1 = arr.Where(New Func(Of Integer, Boolean)(AddressOf Filter))
Dim result11 = arr.Where(AddressOf Filter) 'VB会自生成一个与Func签名相同的委托
Dim result2 = arr.Where(New Func(Of Integer, Boolean)(Function(value)
Return value Mod 2 = 0
End Function))
Dim result3 = arr.Where(
Function(value As Integer)
Return value Mod 2 = 0
End Function)
End Sub
Function Filter(ByVal value As Integer) As Boolean
Return value Mod 2 = 0
End Function
End Module
Dim arr As Integer() = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Sub Main()
Dim result1 = arr.Where(New Func(Of Integer, Boolean)(AddressOf Filter))
Dim result11 = arr.Where(AddressOf Filter) 'VB会自生成一个与Func签名相同的委托
Dim result2 = arr.Where(New Func(Of Integer, Boolean)(Function(value)
Return value Mod 2 = 0
End Function))
Dim result3 = arr.Where(
Function(value As Integer)
Return value Mod 2 = 0
End Function)
End Sub
Function Filter(ByVal value As Integer) As Boolean
Return value Mod 2 = 0
End Function
End Module
两个语言各有优势,缺憾也很明显