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

  • Home
  • SEARCH
  • 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 1056705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:45:17+00:00 2026-05-16T17:45:17+00:00

By way of explanation, take this value type in C#: struct ObjRef { public

  • 0

By way of explanation, take this value type in C#:

struct ObjRef
{
    public object Value;
    public ObjRef(object value) { Value = value; }
}

I can imagine an object graph where there are two boxed instances of this type, each holding a reference to the other. This is what I mean by a reference-cycle with only value-types.

My question is whether or not such an object graph can ever be constructed in .NET. Conceptually, the construction, if it exists, would go like this:

object left = new ObjRef();
object right = new ObjRef(left);
left.Value = right;

but obviously, the last line there is not valid C#. Making the last line:

((ObjRef)left).Value = right;

does not achieve the result as the cast unboxes left and you end up mutating a copy. So at least in straight C#, it doesn’t look like the construction is possible.

Does anybody know if the construction could be achieved using reflection, unsafe code, dynamic, IL code, or in any other manner? Or, can anyone show that the CLR effectively prevents such a reference-cycle?

Please note that I don’t actually want to create such an object graph. Rather, the answer may affect the design of algorithms which work with object graphs, such as serialization/deserialization formatters.


EDIT

As Brian suggested, it is indeed possible to modify the boxed value without unboxing it, by casting it to an interface type instead of the value type. So given this code:

interface IObjRef
{
    IObjRef Value { get; set; }
}

struct ObjRef : IObjRef
{
    IObjRef value;
    public IObjRef Value { get { return value; } set { this.value = value; } }
    public ObjRef(IObjRef value) { this.value = value; }
}

then the reference-cycle I describe can be constructed like this:

IObjRef left = new ObjRef();
IObjRef right = new ObjRef(left);
left.Value = right;

Which basically leaves us with reason #72 why mutable value-types are evil.

  • 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-05-16T17:45:18+00:00Added an answer on May 16, 2026 at 5:45 pm

    This is possible by using an interface and having the value type implement the interface and to refer to each other. This allows them to create a cycle through boxed values since the struct when used with the interfaced reference will be boxed.

    Quick Sample

    interface ICycle
    {
        void SetOther(ICycle other);
    }
    
    struct Cycle : ICycle
    {
        ICycle value;
        public void SetOther(ICycle other)
        {
            value = other;
        }
    }
    
    class Example
    {
        static void CreateCycle()
        {
            ICycle left = new Cycle();   // Left is now boxed
            ICycle right = new Cycle();  // Right is now boxed
            left.SetOther(right);
            right.SetOther(left);  // Cycle
        }
    }
    

    I share Brian’s question though about wondering what advantage this will give you.

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

Sidebar

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.