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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:54:20+00:00 2026-06-01T23:54:20+00:00

When using nested generics, the compiler fails when used directly yet compiles correctly when

  • 0

When using nested generics, the compiler fails when used directly yet compiles correctly when using constraints.

Example:

public static void Test1<V, E>(this Dictionary<V, E> dict)
    where V : IVertex
    where E : IEdge<V>
{}

public static void Test2(this Dictionary<IVertex, IEdge<IVertex>> dict){}

The two extension methods above have ostensibly the same signature, but if I was to now try to run code such as:

var dict = new Dictionary<VertexInstance, EdgeInstance>();

dict.Test1();
dict.Test2();

the compiler would err on ‘Test2’ stating that it could not convert to the generic form with the inline nested generic. Personally I find the syntax for Test2 to be more intuitive.

I posted this originally as an answer in a question which asked about differences between using generic constraints and using interfaces directly, but I am curious as to why this happens?

  • 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-01T23:54:22+00:00Added an answer on June 1, 2026 at 11:54 pm

    Expanding my comment:

    These extension methods of course do not have the same signature. Dictionary<IVertex, IEdge<IVertex>> is not the same as Dictionary<VertexInstance, EdgeInstance>. @Payo is correct; this is a question of variance. Classes cannot be co- or contravariant. Interfaces can, but only if they’re marked for it, and IDictionary cannot be marked for it because it wouldn’t be safe, so changing to IDictionary wouldn’t help here.

    Consider if this were allowed. Your Test2 implementation could look like this:

    public static void Test2(this Dictionary<IVertex, IEdge<IVertex>> dict)
    {
        dict.Add(new EvilVertex(), new EvilEdge());
    } 
    

    EvilVertex and EvilEdge could implement the correct interfaces, but not inherit from VertexInstance and EdgeInstance. The call would then fail at runtime. The call to Test2 is therefore not provably safe, so the compiler does not allow it.

    Thank you for the answer; however, the constraints version could have the same code inside and would have the same issue, wouldn’t it?

    No! The constraints version could not have the same code inside, because you can’t convert from EvilVertex to V, nor from EvilEdge to E. You could force a cast from the type to the type parameter, by casting first to object, but that would of course fail at run time.

    Also, why is variance controlled at that level?

    Because one purpose of generics is to prove the code’s type safety at compile time.

    Your dict.Add should have the compilation error not the extension method in my view.

    As mentioned, the call to dict Add is a compiler error for the generic version. It can’t be a compiler error for the interface version, because in the context of Test2, all you know is that you’re converting EvilVertex to IVertex, which is perfectly legal.

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

Sidebar

Related Questions

What are the pros and cons of using nested public C++ classes and enumerations?
It is often useful to implement algorithms using nested array operations. For example, to
In my asp.net website I am using nested repeaters.Outer repeater is used to display
I'm using nested sets (aka modified preorder tree traversal) to store a list of
I am using nested Asynchronous callbacks to save my front-end data to the back-end
I'm using nested entity groups in an HRD google app engine datastore. A <
I'm using nested pages in Jquery Mobile: Looks like this: // wrap page <div
In runtime I'm creating a DataTable and using nested for-loops to populate the table.
I am currently asked to compare certain images with each other (using nested for
I'm using a nested set in a MySQL table to describe a hierarchy of

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.