udana
udana

Reputation: 17

Action delegate -declaration correction

According to the definition of action delegate it does not return value but passes value.

I pass the value to Console.WriteLine( )

Action<int> an = new Action<int>(Console.WriteLine(3000));

But still i receive error as method name expected.What is the problem?

Upvotes: 0

Views: 327

Answers (3)

Zenuka
Zenuka

Reputation: 3250

Action points to a method only not to any parameters.

You can then use it like this to invoke the action:

Action<int> action = new Action<int>(Console.WriteLine);
action.Invoke(3000);

Upvotes: 0

Chris Dunaway
Chris Dunaway

Reputation: 11216

You would code it like this:

Action<int> an = new Action<int>(Console.WriteLine);
an(3000);

Chris

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

The constructor of Action<int> expects you to pass a pointer to a function that takes an integer as parameter and returns nothing. What you are passing is not a function but an expression. You could either define an anonymous function or use an existing one:

Action<int> an = new Action<int>(t => Console.WriteLine(t));
an(3000);

Upvotes: 3

Related Questions