让说我有一个学生类代表必须是前主要方法
class Program
{
delegate bool del2(Student s); //I have to put this delegate before Main?
static void Main(string[] args)
{
List<Student> listStudent = new List<Student>()
{
... //adding student instances...
};
//delegate bool del2(Student s); Q1: why compile error if I put it here?
Predicate<Student> del1 = check;
Student s = listStudent.Find(del1);
Console.WriteLine("s is" + s.Name);
}
public static bool check(Student s) //Q2:why it need to be static method?
{
return s.Name == "Michael";
}
}
我有两个问题:?
为什么我的主要方法之前把DEL2? del1是一个Predicate委托,我可以把它放在主要方法中,del2也是一个委托,为什么我不能把它放在主要方法里呢?
为什么检查方法是静态的?
它是一种宣言。就像结构或类声明一样。 C#语法要求类型声明不会出现在方法体内。在Main()方法之后移动它*也很好,只是不在里面。 –