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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:24:40+00:00 2026-05-12T12:24:40+00:00

In C++, I can do something like this: int i[10] = new int[10]; int

  • 0

In C++, I can do something like this:

int i[10] = new int[10];
int *p = &i[5];

Then, I can always know that p points to the 5th element of int array i, regardless of i‘s contents.

Is there any way to do something similar in C#?

I realize this is likely one of the ways in which C# "protects" us from ourselves, so I’m not looking for an exact equivalent, but rather a similar concept… that is, being able to refer to the contents of some other variable, rather than the instance of the variable itself.

Here’s my use case I’m thinking of. I have an array of strings. I would like to have another array of references to those array elements. Something like this (obviously not valid code):

string[] s = new string[] { "one", "two", "three", "four", "five", "six" };
stringref[] sr = new stringref[] { &s[0], &s[1], &s[2], &s[3], &s[4], &s[5] };

Console.WriteLine(sr[1]); // == "two"
s[1] = "two point zero";
Console.WriteLine(sr[1]); // == "two point zero"

Certainly, ref parameters do this, and out parameters allow you to write to a specific variable. But what about non-parameters? Can you store a ref? Can you keep an array of refs or a dictionary?

It seems like if the ability to do it with parameters is present, there should be a way to do it without them.

  • 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-12T12:24:41+00:00Added an answer on May 12, 2026 at 12:24 pm

    No. Putting unsafe code aside, which does allow holding pointers to memory locations, there’s no way to store a reference to a variable in C#.

    ref and out arguments provide the only means to take a reference but you can’t save them anywhere.

    You can workaround this limitation by wrapping fields in a class and using its reference instead. This is what the compiler does to capture variables in closures:

    For instance, when you write:

    int integer = 0;
    Action<int> method = i => Console.WriteLine(i + integer);
    integer = 42;
    method(100); // prints 142, not 100
    

    In the second line, the compiler will have to take out the anonymous method and store it as a separate method in the class. Obviously, that method won’t have access to integer variable. It somehow needs to pass a “reference” to integer variable to that anonymous method. Since it’s not possible, it’ll generate a class with a field to hold an integer and uses an instance of that class to store the variable. Basically, the local variable is promoted to a field in a class and is stored in the heap.

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

Sidebar

Related Questions

Problem: When I do something like this: for (int i = 0; i <
I tried to do something like this but this doesn't work: class Garage {
I'm new to Java and even newer to java database connections. I've managed to
intHi, Pretty new to LINQ. I have a Ratings table. A User adds a
Greetings, this is my very first question and I just do programming as a
I have a function GetAlertData() that returns a Datatable. I invoke it as: var
I have an existing application in C++ with a custom ArrayBase class that manages
I have 2 entities Role & Translation. Role -> Role_ID, Code Translation -> Code,
Update OK, after some investigation, and thanks in big part to the helpful answers
Today I made a 64bit build of my project for the first time. Basically

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.