someCombobox.Text = "x" ) I get this er...">

What's wrong with the new action (() => someCombobox.Text = "x")

When I write in my code

Action(() => someCombobox.Text = "x" ) 

I get this error:

The delegate ' System.Action<object> ' does not accept 0 arguments

Why?

This question is related to this . I just want to understand why this error occurs.

+4
source share
3 answers

If you want to create a System.Action delegate that has no parameters and does not return a value, just change the code to this by deleting new Action([body]) :

 Action newAction = () => someCombobox.Text = "x"; 

This is because the lambda expression will return a new delegate for you without System.Action parameters. EDIT: as noted by Aliostad , () => someCombobox.Text = "x" will return either a lambda expression or Action , depending on the type of variable you are assigning.

EDIT: as Darin says, if you want him to accept an argument, you need to pass this when creating the lambda expression.

+5
source

You do not need to pass this as a constructor parameter:

  Action a = () => someCombobox.Text = "x"; 

All you have to do is declare an action and then use the lambda expression to create it.

Alternatively, you can pass the string into action:

  Action<string> a = (s) => someCombobox.Text = s; a("your string here"); 
+8
source

I think the answer here is the same as in the related question you are referring to: .NET 2.0 has only a definition for the Action delegate that accepts a parameter.

In .NET 3.5, a delegate without parameters without restrictions is added. It requires a reference to System.Core.

0
source

Source: https://habr.com/ru/post/1392920/


All Articles