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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:43:51+00:00 2026-06-15T22:43:51+00:00

I’m slightly baffled by some behaviour I don’t understand from the compiler. I’ve reduced

  • 0

I’m slightly baffled by some behaviour I don’t understand from the compiler. I’ve reduced it to the following code sample:

    public interface IFoo { }
    public interface IBar<T> : IFoo { }
    public delegate void DHandler<T>(IBar<T> arg);

    public static class Demo
    {
        static void Garply<T>(DHandler<T> handler) { }

        public static void DoStuffWithInt()
        {
            Garply<int>(Handler);
        }

        static void Handler(IFoo arg) { }
    }

My problem is that I don’t expect the code to compile, but it does. I don’t expect it to compile because DHandler<int> requires IBar<int> in the signature, but the Handler method declares IFoo, which is not IBar<int> (although the converse is true). Hence Handler is not a DHandler<int> and so a delegate to it wouldn’t be usable as an argument to a Garply<int> call.

If I change the code to read Handler(IBar<int> arg) it compiles. If I change it to read Handler(IBar<string> arg) it doesn’t. Both those behaviours are as I’d expect.

The practical problem that prompts this question is that when the signature is Handler(IBar<int> arg), the compiler complains that I need to explicitly specify the type parameter for the Garply call. Trivial in this example, but in the actual code it’ll be a real nuisance. I was mystified, since the argument to Garply is a method with the signature (IBar<int> arg), so the delegate to it would be a DHandler<int>, so the chosen Garply<T> would unambiguously be Garply<int>. But apparently the compiler saw an ambiguity. It was investigating that, that led me to the above puzzle and I can only guess that perhaps the compiler is thinking “well, to Jason’s surprise, I’d have accepted an IFoo for this IBar<T>, so a T has to be specified to allow me to know that I should compile it as IBar<T> and not as IFoo“. That might explain why it wants the type parameter. But can anyone cast light on this?

  • 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-15T22:43:53+00:00Added an answer on June 15, 2026 at 10:43 pm

    This is delegate variance, which was introduced in C# 2. This is not the same as the generic variance introduced in C# 4.

    Here’s an even simpler example:

    delegate void Foo(string x);    
    
    class Test
    {
        static void Main()
        {
            Foo foo = Bar;
        }
    
        static void Bar(object y) {}
    }
    

    The point is that we can create an instance of the Foo delegate from the Bar method because Bar will work provided it’s given any object. When a Foo delegate is called, it will always provide a string reference, and there’s a reference conversion from string to object. So if I have:

    Foo f = ...;
    f("fred");
    

    … that call would always be appropriate for Bar.

    Likewise in your case, any call that Garply<T> makes with handler would definitely be a valid call to Handler – so the compiler is happy to create an appropriate DHandler<T> instance.

    The problem when Handler only accepts an IBar<int> is that the compiler doesn’t use possible method group conversions for arguments when inferring type parameters. This is an area where type inference certainly could be stronger, and indeed it improved – in a very similar area, although I can never remember the details – between C# 3 and C# 4.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I am currently running into a problem where an element is coming back from
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.