Per Jespersen

Per Jespersen

  • NA
  • 23
  • 34.8k

Storing a ref in a class

Sep 21 2010 5:56 AM

Hi

Is it possile to store a reference to an object in a class, so that if the referenced object is changed in by the class, the object is changed everywhere. This could easily be done with a pointer, but they are a bit dirty i c#.

Here is an example of what I mean:

 class MyClass{

public MyClass(ref int RefToObject){
OwnRef = RefToObject;
}
public ref int OwnRef;

}

void main(){

int a = 10;

MyClass b = new MyClass(ref a);

}


I know this won't work, but maybe you can se what I'm trying to do. I want the reference to a stored in b for later mnipulation. Can this be done elegantly perhaps?




Answers (2)