© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
21 replies
MikeS++

A way to store references or pointers to objects in arrays/lists?

Is there a way to do the following code, which is written in c++, in c#?
Assume those variables cannot be inside the array, but have to stay where they are declared somewhere in some class.
int a = 1;
int b = 2;
int c = 3;

std::vector<int*> ListOfVariableReferences;
ListOfVariableReferences.push_back(&a); // Adds the address of a to the list.
ListOfVariableReferences.push_back(&b); // Adds the address of b to the list.
ListOfVariableReferences.push_back(&c); // Adds the address of c to the list.

*ListOfVariableReferences[0] = 5; // Changes the value of the variable a declared at the top
int a = 1;
int b = 2;
int c = 3;

std::vector<int*> ListOfVariableReferences;
ListOfVariableReferences.push_back(&a); // Adds the address of a to the list.
ListOfVariableReferences.push_back(&b); // Adds the address of b to the list.
ListOfVariableReferences.push_back(&c); // Adds the address of c to the list.

*ListOfVariableReferences[0] = 5; // Changes the value of the variable a declared at the top
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Arrays in constructors and lists
C#CC# / help
4y ago
❔ How to store instantiated objects in a delegate?
C#CC# / help
3y ago
❔ “Mini” references? (Managed references with less than 32/64bit pointers. 8bit for example)
C#CC# / help
3y ago