Normally you can do this and pass the reference type by reference and
the new keyword won't make the variables point to different objects:

static void Main(string[] args)
{
Object origObj = new Object();
ExampleMethod(ref origObj);
}
void ExampleMethod(ref Object newObj)
{
//origObj will also reference the newly created object with the following line. origObj and newObj will point to the same object.
newObj = new Object();
}

I'd like to do this, without using a method to do it:

static void Main(string[] args)
{
Object origObj = new Object();
Object newObj;
// something that would mean the same as the following line:
ref origObj = ref newObj;
// origObj also should reference the new object with the following line. Without the above line which isn't valid, newObj and origObj will point to different objects.
newObj  = new Object;
}

Thanks!

Because objects are reference types that is easy, let one object point to the other:

Object origObj = new Object();
Object newObj = origObj; //done!

Thanks for the comment. Unfortunately, the origObj and newObj will point to different objects if you later do this:

        Object origObj = new Object();
        Object newObj = origObj;
        newObj = new object();    
      //origObj != newObj

Whereas, when using a method and the ref keyword, then again do this, they will continue to reference the same object:

        void methodA(ref newObj)
        {
        newObj = new object();
        }   

    //origObj == newObj

Yes, because newObj IN methodA will no longer exist after the method finishes. So newObj OUT of the method will still point to origObj.
Might I ask why you want to do this kind of thing?

This article has been dead for over six months. Start a new discussion instead.