In C#, all instance variables are pointers. In fact, when you pass an object as a method's argument, you are passing a new pointer to the same object. This way, the object is never passed by value (see NOTE), and you are able to modify the same instance from different scopes.
Another important thing to note:
public void SomeMethod(object instance)
{
instance = null;
}
In here you are not setting the instance to null
, but instead, changing your pointer to point to a null
value, so your original instance
will remain untouched.
public void SomeMethod(object instance)
{
instance = new object();
}
In here you are not modifying the instance
value either. You are just changing your pointer to point to a new completely object, leaving the original instance
untouched again.
NOTE: Although objects are never passed by value, the pointers to them actually are. As I said, when passing an object to a method, you are creating a new pointer to it, so you can think of the pointer being passed by value.