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

    Expert Comment

    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

    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

    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

    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

    got my solution with this answer.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now