Posted on 2011-04-22
Medium Priority
Last Modified: 2012-06-27
what does delegate( int y= function(2,3) ) mean?
Question by:rnsr
  • 3
  • 2
LVL 11

Expert Comment

ID: 35452098
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....




Author Comment

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

i wnat to know what is the role of delegate here.
LVL 11

Accepted Solution

saragani earned 1000 total points
ID: 35452169
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 ....)
LVL 11

Expert Comment

ID: 35452171
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).

Author Closing Comment

ID: 35465722
got my solution with this answer.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question