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 ?

Signature
Madhur
http://madhurahuja.blogspot.com
Marc Gravell - 12 Jan 2008 20:16 GMT
> 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
Nicholas Paldino [.NET/C# MVP] - 12 Jan 2008 21:01 GMT
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.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
>> This is kind of a deadlock. Isnt it ?
>
[quoted text clipped - 9 lines]
>
> Marc
Marc Gravell - 12 Jan 2008 23:54 GMT
> 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
Arne Vajhøj - 12 Jan 2008 20:17 GMT
> If I define two classes in the same cs file. And in each class, I define the
> object of other class as a member.
[quoted text clipped - 3 lines]
>
> 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
Marc Gravell - 12 Jan 2008 20:22 GMT
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