C# 中的简单匿名方法
请参阅下面的第二段代码......代码无法编译。 我试图找出匿名方法,我明白了..
但不是我在网上找到的不使用匿名方法的例子,它不能编译
使用VS2008..编译为.NET3.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestAnonymousMethods
{
public class Program
{
// using an anon method
static void Mainx(string[] args)
{
int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] evenIntegers = Array.FindAll(_integers,
// this is the anonymous method below
delegate(int integer)
{
return (integer % 2 == 0);
}
);
foreach (int integer in _integers)
Console.WriteLine(integer);
foreach (int integer in evenIntegers)
Console.WriteLine(integer);
}
// not using anon method
static void Main(string[] args)
{
int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] evenIntegers = Array.FindAll(_integers, IsEven); // **Compile error here**
foreach (int integer in _integers)
Console.WriteLine(integer);
foreach (int integer in evenIntegers)
Console.WriteLine(integer);
}
public bool IsEven(int integer)
{
return (integer % 2 == 0);
}
}
}
See the second bit of code below.. code doesn't compile. Am trying to figure out anon methods, and I get it..
But not the example of not using anon methods which I found on the web, which doesn't compile
Using VS2008.. compiling to .NET3.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestAnonymousMethods
{
public class Program
{
// using an anon method
static void Mainx(string[] args)
{
int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] evenIntegers = Array.FindAll(_integers,
// this is the anonymous method below
delegate(int integer)
{
return (integer % 2 == 0);
}
);
foreach (int integer in _integers)
Console.WriteLine(integer);
foreach (int integer in evenIntegers)
Console.WriteLine(integer);
}
// not using anon method
static void Main(string[] args)
{
int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] evenIntegers = Array.FindAll(_integers, IsEven); // **Compile error here**
foreach (int integer in _integers)
Console.WriteLine(integer);
foreach (int integer in evenIntegers)
Console.WriteLine(integer);
}
public bool IsEven(int integer)
{
return (integer % 2 == 0);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Main 是静态的,因此 IsEven 也必须是静态的。
Main is static so IsEven must be static too.