Phil Whittaker
Phil Whittaker

Reputation: 434

Func<T> scope inside class

Why can't I do this?

public int FillModel(int id)
{
   // do something...
   return 0;
}

public Func<int, int> actiontest = FilleModel;

The code doesn't compile and tells me there is no reference?

Upvotes: 0

Views: 101

Answers (2)

Omar
Omar

Reputation: 16623

As said L.B in his comment you should change:

public Func<int, int> actiontest = FilleModel;  //FilleModel doesn't exist

with:

Func<int, int> actiontest = FillModel;

Else if you want to make it public:

public Func<int, int> actiontest;

public myClass(){
   actiontest = FillModel;
}

Or:

public Func<int, int> actiontest = FillModel;

private static int FillModel(int id) //private else the delegate doesn't make sense
{
   // do something...
   return 0;
}

Upvotes: 2

Furqan Safdar
Furqan Safdar

Reputation: 16708

One Important thing, apart from changing:

public Func<int, int> actiontest = FilleModel; 

to

Func<int, int> actiontest = FillModel;

You cannot have a direct declaration at the Class level. You can only have such declaration inside some behavior Method or Property setter/getter.

Upvotes: 0

Related Questions