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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:00:49+00:00 2026-06-17T09:00:49+00:00

Scenario is very rare, but quite simple: you define a generic class, then create

  • 0

Scenario is very rare, but quite simple: you define a generic class, then create a nested class which inherits from outer class and define a associative field (of self type) within nested. Code snippet is simpler, than description:

class Outer<T>
{
    class Inner : Outer<Inner>
    {
        Inner field;
    }
}

after decompilation of IL, C# code look like this:

internal class Outer<T>
{
    private class Inner : Outer<Outer<T>.Inner>
    {
        private Outer<Outer<T>.Inner>.Inner field;
    }
}

This seems to be fair enough, but when you change the type declaration of the field, things become trickier. So when I change the field declaration to

Inner.Inner field;

After decompilation this field will looks like this:

private Outer<Outer<Outer<T>.Inner>.Inner>.Inner field;

I understand, that class ‘nestedness’ and inheritance don’t quite get along with each other, but why do we observe such behavior? Is the Inner.Inner type declaration has changed the type at all? Are Inner.Inner and Inner types differ in some way in this context?

When things become very tricky

You can see the decompiled source code for the class below. It’s really huge and has total length of 12159 symbols.

class X<A, B, C>
{
    class Y : X<Y, Y, Y>
    {
        Y.Y.Y.Y.Y.Y y;
    }
} 

Finally, this class:

class X<A, B, C, D, E>
{
    class Y : X<Y, Y, Y, Y, Y>
    {
        Y.Y.Y.Y.Y.Y.Y.Y.Y y;
    }
}

results in 27.9 MB (29,302,272 bytes) assembly and Total build time: 00:43.619

Tools used

Compilation is done under C# 5 and C# 4 compilers. Decompilation is done by dotPeek. Build configurations: Release and Debug

  • 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-17T09:00:50+00:00Added an answer on June 17, 2026 at 9:00 am

    The core of your question is why Inner.Inner is a different type than Inner. Once you understand that, your observations about compile time and generated IL code size follow easily.

    The first thing to note is that when you have this declaration

    public class X<T>
    {
      public class Y { }
    }
    

    There are infinitely many types associated with the name Y. There is one for each generic type argument T, so X<int>.Y is different than X<object>.Y, and, important for later, X<X<T>>.Y is a different type than X<T>.Y for all T‘s. You can test this for various types T.

    The next thing to note is that in

    public class A
    {
      public class B : A { }
    }
    

    There are infinitely many ways to refer to nested type B. One is A.B, another is A.B.B, and so on. The statement typeof(A.B) == typeof(A.B.B) returns true.

    When you combine these two, the way you have done, something interesting happens. The type Outer<T>.Inner is not the same type as Outer<T>.Inner.Inner. Outer<T>.Inner is a subclass of Outer<Outer<T>.Inner> while Outer<T>.Inner.Inner is a subclass of Outer<Outer<Outer<T>.Inner>.Inner>, which we established before as being different from Outer<T>.Inner. So Outer<T>.Inner.Inner and Outer<T>.Inner are referring to different types.

    When generating IL, the compiler always uses fully qualified names for types. You have cleverly found a way to refer to types with names whose lengths that grow at exponential rates. That is why as you increase the generic arity of Outer or add additional levels .Y to the field field in Inner the output IL size and compile time grow so quickly.

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

Sidebar

Related Questions

I have a very simple scenario but poor experience in this area and googling
I have a very simple scenario, using NHibernate: one abstract base class animal; two
I have a very simple scenario which I cannot get to work. I am
I have a very simple scenario: My iPhone application contains a UIViewController implementation which
It may be very simple scenario for experienced developers, but as a beginner I
we have a (in our oppinion) very simple scenario here. But we got stuck
I have very simple scenario where class A registers instances for types. A.register(T1.class, new
This is a very common scenario: displaying images in a ListView which have to
I have a very simple scenario involving a database and a JMS in an
Having some trouble with what should be a very simple scenario. For example purposes,

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.