Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

how .net references circular references

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Madhur - 12 Jan 2008 20:07 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.