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 8482919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:56:36+00:00 2026-06-10T19:56:36+00:00

How in C# do you create a true singlecast delegate. i.e a delegate instance

  • 0

How in C# do you create a true singlecast delegate. i.e a delegate instance that can reference one (and only one) method in its invocation list and so be used as a callback (for a single subscriber) rather than an event (which may have many subscribers).

The Framework has the classes System.Delegate and System.MulticastDelegate, which gives the mistaken impression that System.Delegate is singlecast and System.Multicast delegate adds multicast capability. But the MSDN documentation for System.Delegate http://msdn.microsoft.com/en-us/library/system.delegate.aspx indicates that System.Delegate is in fact multicast…

“The invocation list of a delegate is an ordered set of delegates in which each element of the list invokes exactly one of the methods represented by the delegate.”

…while the documentation for System.MulticastDelegate does not really explain what extra behaviour it provides.

The official documentation in this area is pretty confusing, but one thing which is clear is that end-users cannot derive from either System.Delegate or System.MulticastDelegate. So is there any way supported by the framework to create a true singlecast delegate that can be used as a variable to store a reference to a single callback?

@dtb. If I can use a singlecast delegate then the need for the runtime check is eliminated. Of course it’s true that the application logic could still fail in other ways, such as the wrong handler being assigned, but at least if I use a singlecast delegate then the problem of there being multiple handlers where I only expect one is one problem that simply cannot exist, hence there is one less thing to check, simpler unit tests, more elegant design. Also if a delegate for a method with a return value has multiple handlers in its invocation list, then it is the value returned by the last handler in the list that is returned to the caller, not the first.

  • 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-06-10T19:56:38+00:00Added an answer on June 10, 2026 at 7:56 pm

    If you really need a “singlecast” delegate with an event why don’t you simply implement your own add/remove methods for that event?

    The “problem” of Delegate is that MulticastDelegate is a derived class so if anyone assigns a MulticastDelegate object to a Delegate variable then you’ll always have a MulticastDelegate.

    For example we can simplify the default event implementation to this:

    private ChangedEventHandler _changed;
    public event ChangedEventHandler Changed
    {
       add
       {
          _changed += value;
       }
       remove
       {
          _changed -= value;
       }
    }
    

    Now let’s change the event implementation to:

    private ChangedEventHandler _changed;
    public event ChangedEventHandler Changed
    {
       add
       {
          _changed = value; // Do NOT combine delegates
       }
       remove
       {
          _changed -= value;
       }
    }
    

    Now what you have is (almost) a singlecast delegate, because of event syntax users can’t assign directly a multicast delegate and only the last assigned delegate is stored. If your users are really malicious they can create a MulticastDelegate and then add that delegate to your event handler. If you really need to prevent this you may add this check to the add method:

    if (value.GetInvocationList().Length > 1)
        throw new ArgumentException("MulticastDelegates are not allowed here.");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TObject list ( FileEventObjects := TObjectList.Create(True); ) containing one or more
If I create a SslStream instance like this: secureStream = new SslStream(stream, true, tlsRemoteCallback,
I understood how to create events, like that: var evt=document.createEvent('Event'); evt.initEvent('foo',true,true); window.dispatch(evt); Then anyone
I'm attempting to create a true mosaic application. At the moment I have one
I used this tutorial http://delphi.about.com/od/kbthread/a/thread-gui.htm to create a class that asynchronously downloads a file
I need to create a json message like this: { success:true, count:3, data: [
2 panels: Ext.create('Ext.Container', { fullscreen: true, layout: 'hbox', items: [ { xtype: 'panel', html:
If I have something like this: var container = Ext.create('Ext.container.Container', { plain:true, width :
Whats wrong with this picture? Model: validates_acceptance_of :terms_of_service, :on => :create, :accept => true,
I used schemaExport to create my tables like this Configuration config = new Configuration();

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.