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 1049517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:37:44+00:00 2026-05-16T16:37:44+00:00

We know that int is a value type and so the following makes sense:

  • 0

We know that int is a value type and so the following makes sense:

int x = 3;
int y = x;
y = 5;
Console.WriteLine(x); //says 3. 

Now, here is a bit of code where we want to for lack of a better term “link” the two variables point to the same memory location.

int x = 3;
var y = MagicUtilClass.linkVariable(() => x);
y.Value = 5;
Console.WriteLine(x) //says 5.

The question is: How does the method linkVariable look like? What would it’s return type look like?

Although, I titled the post as making a value type behave as a reference type, the said linkVariable method works for reference types too.., i.e,

Person x = new Person { Name = "Foo" };
var y = MagicUtilClass.linkVariable(() => x);
y.Value = new Person { Name = "Bar" };
Console.WriteLine(x.Name) //says Bar.

I am not sure how to achieve this in C# (not allowed to use unsafe code by the way)?

Appreciate ideas. Thanks.

  • 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-16T16:37:44+00:00Added an answer on May 16, 2026 at 4:37 pm

    Here is a full solution:

    // Credits to digEmAll for the following code
    public delegate void Setter<T>(T newValue);
    public delegate T Getter<T>();
    public class MagicPointer<T>
    {
        private Getter<T> getter;
        private Setter<T> setter;
    
        public T Value
        {
            get { return getter(); }
            set { setter(value); }
        }
    
        public MagicPointer(Getter<T> getter, Setter<T> setter)
        {
            this.getter = getter;
            this.setter = setter;
        }
    }
    
    // Code starting from here is mine
    public static class MagicUtilClass
    {
        public static MagicPointer<T> LinkVariable<T>(Expression<Func<T>> expression)
        {
            var memberExpr = expression.Body as MemberExpression;
            if (memberExpr == null)
                throw new InvalidOperationException("The body of the expression is expected to be a member-access expression.");
            var field = memberExpr.Member as FieldInfo;
            if (field == null)
                throw new InvalidOperationException("The body of the expression is expected to be a member-access expression that accesses a field.");
            var constant = memberExpr.Expression as ConstantExpression;
            if (constant == null)
                throw new InvalidOperationException("The body of the expression is expected to be a member-access expression that accesses a field on a constant expression.");
            return new MagicPointer<T>(() => (T) field.GetValue(constant.Value),
                                       x => field.SetValue(constant.Value, x));
        }
    }
    

    Usage:

    int x = 47;
    var magic = MagicUtilClass.LinkVariable(() => x);
    magic.Value = 48;
    Console.WriteLine(x);  // Outputs 48
    

    To understand why this solution works, you need to know that the compiler transforms your code quite considerably whenever you use a variable inside a lambda expression (irrespective of whether that lambda expression becomes a delegate or an expression tree). It actually generates a new class containing a field. The variable x is removed and replaced with that field. The Usage example will then look something like this:

    CompilerGeneratedClass1 locals = new CompilerGeneratedClass1();
    locals.x = 47;
    var magic = MagicUtilClass.LinkVariable(() => locals.x);
    // etc.
    

    The “field” that the code retrieves is the field containing x, and the “constant” that it retrieves is the locals instance.

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

Sidebar

Related Questions

Given int a; , I know that the following returns the largest value that
I know that for an integer, you can use: int value; I tried: string
I know I succeed in writing my code to that address using int 13h
Consider the following code sample that uses MEF to create an object of type
Consider the following code that automates generation of Boost.MPL type sequences (list or vector).
So I know that C++ has an Operator Precedence and that int x =
I know that static const int x = 42; at namespace scope is equivalent
I know that a char and an int are calculated as being 8 bytes
I know that I can get a POSIX int file handle from NSURL by
I know that if I do something like this: class Obj { public: int*

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.