为什么我不能这样做?
public int FillModel(int id)
{
// do something...
return 0;
}
public Func<int, int> actiontest = FilleModel;
代码无法编译并告诉我没有参考?
正如LB在他的评论中所说,你应该改变:
public Func<int, int> actiontest = FilleModel; //FilleModel doesn't exist
和:
Func<int, int> actiontest = FillModel;
否则,如果您想公开:
public Func<int, int> actiontest;
public myClass(){
actiontest = FillModel;
}
或者:
public Func<int, int> actiontest = FillModel;
private static int FillModel(int id) //private else the delegate doesn't make sense
{
// do something...
return 0;
}
一件重要的事情,除了改变:
public Func<int, int> actiontest = FilleModel;
到
Func<int, int> actiontest = FillModel;
您不能在Class 级别进行直接声明。您只能在某些行为或setter/getter 中使用此类声明。Method
Property