how .net references circular references

M

Madhur

Hello

If I define two classes in the same cs file. And in each class, I define the
object of other class as a member.

Can anyone explain me how .NET or its compiler will resolve this kind of
reference since one class would not be compiled unless other is compiled.

This is kind of a deadlock. Isnt it ?
 
M

Marc Gravell

This is kind of a deadlock. Isnt it ?

No - that's fine, as long as they are in the same assembly - the
compiler /broadly/ uses a place-holder in the interim. For example,
all objects have "System.String ToString()" and "System.Int23
GetHashCode()"; which do you build first? System.String, or
System.Int32? or System.Object (since both inherit from object (give-
or-take)) or all at once?

Circular references between *assemblies* are tougher. You can do it at
the command line, but I don't recommend it...

Marc
 
A

Arne Vajhøj

Madhur said:
If I define two classes in the same cs file. And in each class, I define the
object of other class as a member.

Can anyone explain me how .NET or its compiler will resolve this kind of
reference since one class would not be compiled unless other is compiled.

This is kind of a deadlock. Isnt it ?

They are compiled together. Logically it is not a problem.

Compiler implementation wise they must either do multiple
pass or delay check until everything is processed.

Arne
 
M

Marc Gravell

One caveat - you can't have 2 structs that contain each-other as
fields - simply because it makes no sense - how big would the object
be? Not an issue with classes, since there is just a /reference/ to
the other .

Marc
 
N

Nicholas Paldino [.NET/C# MVP]

Circular references between assemblies are not possible in .NET. That
doesn't mean you can't access the types between two assemblies, you just
have to abstract out the types to another shared assembly and have an
external assembly be the factory for the two abstractions.

But at that point, it's not a circular reference anymore.
 
M

Marc Gravell

    Circular references between assemblies are not possible in .NET.
It isn't supported by the IDE, but it is (I believe) possible.
Meaning: I've just done it...

The painful thing is that you must (possibly with #if etc) find a way
of building the two assemblies upwards incrementally such that at each
point at least one of them builds - i.e. I compiled c1.dll with:

using System;
public class A {
}

Then I compiled c2.dll (referencing c1.dll) with:
using System;
public class B {
A test;
}

Then I re-compiled c1.dll (referencing c2.dll) with:
using System;
public class A {
B test;
}

Et voila; circular reference. Very painful, I'll grant - but not
impossible.
Actually, I believe that if you ask the reflection API, there is an
implied circular reference between mscorlib.dll and System.dll - but
reflector doesn't show this, so maybe this one is just the way that
reflection displays it... I believe I found this the hard way when
walking the reference tree (to force-load all libs) - it blew the
stack because of the loop.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top