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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:40:47+00:00 2026-06-01T10:40:47+00:00

I’ve been trying to create a generic event. Basically it should look like this:

  • 0

I’ve been trying to create a generic event. Basically it should look like this:

namespace DelegateTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var lol = new SomeClass();
            lol.SomeEvent += handler;
        }

        static void handler(object sender, SomeDerivedClass e)
        {

        }

    }

    class SomeClass
    {

        public delegate void SomeEventDelegate<in T>(object sender, T data);
        public event SomeEventDelegate<ISomeInterface> SomeEvent;

    }

    interface ISomeInterface
    {
    }

    class SomeDerivedClass : ISomeInterface
    {
    }
}

I want to allow the user to pass any delegate which’s second parameter is derived from “ISomeInterface.”

“in” specifies contra-variance, right? That means if the API is expecting something more general, you can pass it something more specific (in my base “ISomeInterface” would be general, and my “SomeDerivedClass” would be specific.)
I am, however, being told my the compiler that “no overload for method handler matches DelegateTest.SomeClass.SomeEventDelegate.”

I am wondering why this isn’t working. What are the problems that would be caused if it was? Or am I missing something for it to work?

Thanks in advance!

  • 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-01T10:40:49+00:00Added an answer on June 1, 2026 at 10:40 am

    “in” specifies contra-variance, right?

    Yes.

    That means if the API is expecting something more general, you can pass it something more specific (in my base “ISomeInterface” would be general, and my “SomeDerivedClass” would be specific).

    No. Delegate contravariance allows a delegate to reference a method with parameter types that are less derived than in the delegate type. For example, suppose ISomeInterface had a base interface:

    interface ISomeBaseInterface
    {
    }
    
    interface ISomeInterface : ISomeBaseInterface
    {
    }
    

    And suppose handler took ISomeBaseInterface instead of SomeDerivedClass:

    static void handler(object sender, ISomeBaseInterface e) 
    

    Then new SomeClass().SomeEvent += handler would work.

    Here’s why the original code isn’t type safe: When SomeClass raises SomeEvent, it can potentially pass anything that implements ISomeInterface as the data argument. For example, it could pass an instance of SomeDerivedClass, but it could also pass an instance of

    class SomeOtherDerivedClass : ISomeInterface
    {
    }
    

    If you were able to register void handler(object sender, SomeDerivedClass e) with the event, that handler would wind up being invoked with SomeOtherDerivedClass, which doesn’t work.

    In summary, you can register event handlers that are more general than the event type, not event handlers that are more specific.

    UPDATE: You commented:

    Well, I actually want to iterate through the list and check the types. So if the event was to be fired with a data object of type let’s say SomeOtherDerivedObject, then the program would iterate through the list of methods that are subscribed to the event until it finds one that matches the signature (object, SomeOtherDerivedObject). So the event itself would only be used to store, not to actually call the delegates.

    I don’t think C# lets you declare an event that works with arbitrary delegate types. Here’s how you can write methods that add event handlers and invoke them:

    class SomeClass
    {
        private Delegate handlers;
    
        public delegate void SomeEventDelegate<in T>(object sender, T data);
    
        public void AddSomeEventHandler<T>(SomeEventDelegate<T> handler)
        {
            this.handlers = Delegate.Combine(this.handlers, handler);
        }
    
        protected void OnSomeEvent<T>(T data)
        {
            if (this.handlers != null)
            {
                foreach (SomeEventDelegate<T> handler in
                    this.handlers.GetInvocationList().OfType<SomeEventDelegate<T>>())
                {
                    handler(this, data);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

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.