MikeS++
MikeS++
CC#
Created by MikeS++ on 11/12/2023 in #help
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.
c++
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++
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
22 replies