C # - How do self-regulation classes or circular reference classes in the same assembly successfully compile

I would like to know how the C # and .Net compiler can successfully compile classes with binding to classes or circular reference classes within the same assembly.

Consider the following code present in the same assembly.

class X{ X x; }
class Y{ Z z; }
class Z{ Y y; }

Of course, this code compiles successfully.
But how? I would like to know how the compiler can resolve classes in these cases for the first time.
For example, when the compiler encounters class Y, it does not yet know class Z. How can it resolve the child property z in class Y?
Please explain what happens in the background when compiling the code. Probably some suitable articles on how the compiler resolves classes and types

+4
source share
1 answer

As mentioned in this article, the C # compiler performs "Two Pass", i.e.

  • First, it calculates metadata: top-level material, such as namespaces, classes, structures, enumerations, interfaces, delegates, methods, type parameters, formal parameters, constructors, events, attributes, etc.,

  • The second pass computes IL: the code that goes in the bodies of the method, the body of the constructor, etc.

# C/++, , , .. , , .. , . # , Two Pass .

0

All Articles