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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:30:20+00:00 2026-05-14T21:30:20+00:00

I was wondering how one could store a reference to an object in .net.

  • 0

I was wondering how one could store a reference to an object in .net.

That is, I would like something like the following code (note, of course, that the following code may be way off from how to actually do it):

class Test
{
    private /*reference to*/ Object a;
    public Test(ref int a)
    {
        this.a = a;
        this.a = ((int)this.a) + 1;
    }
    public Object getA() { return this.a; }
}
/*
 * ...
 */
static void Main(string[] args)
{
    int a;
    a=3;
    Test t = new Test(ref a);
    Console.WriteLine(a);
    Console.WriteLine(t.getA());
    Console.ReadKey();
}

To produce the following output:

4
4

Ideally, I would like to do this without writing a wrapper class around the integer.

In other words, I think I want pointers in .Net.

  • 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-14T21:30:21+00:00Added an answer on May 14, 2026 at 9:30 pm

    You cannot store references to variables in .NET, period. You can store references to objects, but not references to variables.

    The reason is that if you were allowed to store references to arbitrary variables then you could store references to local variables. If you can store references to local variables then the runtime cannot use the optimization of storing local variables on the short-lived memory pool, aka, the stack.

    Now, even if you could do that, the operation you are describing is not typesafe for a different reason. You have a (very badly named) field variable “a” of type “reference to object variable” and a (very badly and confusingly named) local variable “a” of type “reference to int variable”. Even if you could store a reference to a variable it doesn’t make any sense to store a reference to an int variable in something of type “reference to object variable” because those two types are logically not compatible. The operations you can perform on them are different; a reference to an object variable can have a string written into it; a reference to an int variable cannot.

    Perhaps I am misunderstanding but wouldn’t a variable such as the integer above be boxed into an object which could then be stored as a reference?

    You are confusing references to objects with references to variables. It is confusing that we use the same terminology for what is really two different things.

    Yes, boxing turns a value type, like int, into a reference type, like object. That has ABSOLUTELY NOTHING WHATSOEVER to do with references to variables.

    When you make a ref to a variable you are making an alias for that variable. When you say

    void M(ref int y) { y = 123; }
    ...
    int x = 0;
    M(ref x);
    

    you are saying “x and y are two different names for the same variable”.

    Now, if what you want to do is represent the notion of “I have captured a variable and I want to be able to read and write it” then use delegates:

    class Ref<T>
    {
        private Func<T> getter;
        private Action<T> setter;
        public Ref(Func<T> getter, Action<T> setter)
        {
            this.getter = getter;
            this.setter = setter;
        }
        public T Value
        {
            get { return getter(); }
            set { setter(value); }
        }
    }
    ...
    int abc = 123;
    var refabc = new Ref<int>(()=>abc, x=>{abc=x;});
    ... now you can pass around refabc, store it in a field, and so on
    refabc.Value = 456;
    Console.WriteLine(abc); // 456
    Console.WriteLine(refabc.Value); // 456
    

    Make sense?

    • 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.