what does delegate( int y= function(2,3) ) mean?
Who is Participating?
It is for defining an anonymous method. In this case, a parameter-less void function.
It is hard for me to give you some concrete example that will make you understand what are the uses for it, but lets say that you write a WinForms application which has some functions called in a different thread. (because those functions takes a while to run and you don't want to freeze the GUI).

When the functions are completed, you will want to refresh the GUI, I mean, update some labels, text boxes, comboboxes etc....

Assuming that you have 10 asynchronous functions that at the end of each of them you do something else (in one you need to update the text of the label, in another one show a message box, in the third update a combobox etc)...
Lets say that you also have one of the functions, while it is running.... update the value of a progressbar.

As you know, you should not access UI controls from another thread, cause else some bad thing will happen.
Therefore you need to do that you want to do in the UI thread by calling in the Form:
this.Invoke( A delegate a function that does what you want)

1 option for example is defining 10 types of different delegate signatures and creating 10 functions with those signatures. Then when you want to run those functions in order to invoke the UI, then you need to create an instance of one of those signatures and point it to one of the functions that you have created and then invoke it.

With anonymous method you don't need to create any of those signatures or functions.

You can just do:

Action a = delegate()
    ProgressBar1.Value = 5;


But that's not the only use of anonymous methods.... But the idea is letting you define functions inside a code of another function and without the need of writing:
private void someFunction(arg a, arg b ....)
Are you sure it is written like that?
Maybe it is written like this: delegate() { int y = function(2,3); };

In this way, you create an anonymous method. by

            Action a = delegate()
                 int y = function(2,3);
                 int j = function(4,5);
                 int z = y * j;
                 int k = y + j;
                 // The rest of the code here....



rnsrAuthor Commented:
you are right its like this
delegate() { int y = function(2,3); };

i wnat to know what is the role of delegate here.
And I can't tell what is the role of the code you wrote without seeing the rest of the code.
Someone/something must do something with this delegate (invoke it somewhere).
rnsrAuthor Commented:
got my solution with this answer.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.