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

  • Home
  • SEARCH
  • 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 1107261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:54:45+00:00 2026-05-17T01:54:45+00:00

Is it possible in C# to pass a delegate or event to a method

  • 0

Is it possible in C# to pass a delegate or event to a method so that the method can assign a new event handler to that delegate with += (and not so that the method can call the delegate)?

Lets say I can mix C++ with C#. This would be what I’m looking for:

public class MyClass
{
    public Action* actionPtr;

    public void Assign(Action* action)
    {
        actionPtr = action;
        (*action) += SomeMethod;
    }

    public void Unassign()
    {
        (*action) -= SomeMethod;
    }

    void SomeMethod()
    {
        // Do stuff
    }
}

Hope it makes sense.

  • 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-17T01:54:45+00:00Added an answer on May 17, 2026 at 1:54 am

    Event: no (unless you’re in the class where the event is defined). This is enforced by the C# compiler.

    Plain old delegate: yes. Pass it as a ref parameter.

    (Think about it this way: how do you add a handler to a delegate? You use +=, right? That is an assignment operator, which is static: you are assigning the delegate to a new delegate instance that includes the method specified to the right of the +=, just like x += 1 assigns x to x + 1. The only time you can ever assign an external variable to a new value or object from within a method is when it was passed as a ref or out parameter).

    For example, the following code leaves list untouched:

    List<int> list = null;
    
    InitializeListImproperly(list);
    
    static void InitializeListImproperly(List<int> x)
    {
        // x is a local variable, so this does nothing to the list variable
        // outside this scope!
        x = new List<int> { 1, 2, 3 };
    }
    

    The following would assign it to a new List<int>:

    List<int> list = null;
    
    InitializeListProperly(ref list);
    
    static void InitializeListProperly(ref List<int> x)
    {
        x = new List<int> { 1, 2, 3 };
    }
    

    It’s the same principle.

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

Sidebar

Related Questions

Possible Duplicate: How to pass an event to a method? Is it possible to
Is it possible to pass a delegate to a WCF remote object from the
Possible Duplicate: pass by reference not working I was going through some of the
Is it possible to pass a request from java servlet and read that request
Is it possible to pass additional arguments to an event callback? For example, if
In C# can I pass a delegate an argument list for a class constructor
Possible Duplicate: Pass a buffer by reference to other function When I call the
Possible Duplicate: Why does asynchronous delegate method require calling EndInvoke? Is Delegate.EndInvoke() really necessary?
In C# is it possible to pass a class function(not static) as a parameter
I have this method with its delegate that is used to append text to

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.