Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 487015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:33:43+00:00 2026-05-13T01:33:43+00:00

I`m having some trouble in understanding how delegates in C# work. I have many

  • 0

I`m having some trouble in understanding how delegates in C# work. I have many code examples, but i still could not grasp it properly.

Can someone explain it to me in “plain english”? Of course! examples of code will help, but i think i need more of a description of how/why it works.

EDIT:

Well, the question is: why does delegates work? What is a “flow chart” of the entire process?

What are the pre-requisites of using delegates?

I hope this makes the question clearer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:33:43+00:00Added an answer on May 13, 2026 at 1:33 am

    One way to think about a delegate is like a reference to a function. For example, say you have a button in a window, and you want something to happen when the button is clicked. You can attach a delegate to the Click event of the button, and whenever the user clicks this button, your function will be executed.

    class MyWindow : Window
    {
        Button _button;
    
        public MyWindow()
        {
            _button = new Button();
            // place the button in the window
            _button.Click += MyWindow.ButtonClicked;
        }
    
        static void ButtonClicked(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Button Clicked");
        }
    }
    

    Notice how I make ButtonClicked a static function – I want to make a point about non-static functions next. Suppose instead that ButtonClicked is a non-static member:

    class MyWindow : Window
    {
        Button _button;
        int _numClicked = 0;
    
        public MyWindow()
        {
            this._button = new Button();
            // place the button in the window
            this._button.Click += this.ButtonClicked;
        }
    
        void ButtonClicked(object sender, RoutedEventArgs e)
        {
            this._numClicked += 1;
            MessageBox.Show("Button Clicked " + this._numClicked + " times");
        }
    }
    

    Now the delegate contains both a reference to the function “ButtonClicked” and the instance, “this”, which the method is called on. The instance “this” in the MyWindow constructor and “this” in ButtonClicked are the same.

    This is a specific case of a concept known as closures which allows “saving” the state – the current object, local variables, etc. – when creating a delegate. In the above example, we used “this” from the constructor in the delegate. We can do more than that:

    class MyWindow : Window
    {
        Button _button;
        int _numClicked = 0;
    
        public MyWindow(string localStringParam)
        {
            string localStringVar = "a local variable";
            this._button = new Button();
            // place the button in the window
            this._button.Click += new RoutedEventHandler(
                delegate(object sender, RoutedEventArgs args)
                {
                    this._numClicked += 1;
                    MessageBox.Show("Param was: " + localStringParam + 
                         " and local var " + localStringVar +
                         " button clicked " + this._numClicked + " times");
                });
        }
    }
    

    Here we created an anonymous delegate – a function which is not given an explicit name. The only way to refer to this function is using the RoutedEventHandler delegate object. Furthermore, this function exists in the scope of the MyWindow constructor, so it can access all local parameters, variables, and the member instance “this”. It will continue to hold references to the local variables and parameters even after the MyWindow constructor exits.

    As a side note, the delegate will also hold a reference to the object instance – “this” – even after all other references to the class a removed. Therefore, to ensure that a class is garbage collected, all delegates to a non-static member method (or delegates created in the scope of one) should be removed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.