C#对象参考? [英] C# Object Reference?

查看:65
本文介绍了C#对象参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可以在带有指针的C ++中使用但不知道如何在C#中进行。


简化的示例代码和注释


//创建对象A

对象A =新对象();


//创建对象B

对象B = A;


//创建对象C

对象C =新对象();


// I需要更改对象A以仅使用对象B指向对象C的内容

I know this is possible in C++ with pointers but dont know how to do it in C#.

Simplified Sample Code and Comments

//create object A
object A = new object();

//create object B
object B = A;

//create object C
object C = new object();

//I need to change Object A to point to the contents of Object C only using Object B

推荐答案

为什么需要两个指向同一对象的东西?我无法想出它。
Why do you need two things pointing to the same object? I''ve not been able to come up with a use for it.



我知道这可以在带有指针的C ++中实现,但不要知道怎么用C#来做。


简化示例代码和注释


//创建对象A

对象A =新对象();


//创建对象B

对象B = A;


//创建对象C

对象C =新对象();


//我需要更改对象A以指向仅对象C的内容使用对象B
I know this is possible in C++ with pointers but dont know how to do it in C#.

Simplified Sample Code and Comments

//create object A
object A = new object();

//create object B
object B = A;

//create object C
object C = new object();

//I need to change Object A to point to the contents of Object C only using Object B



嗯,不知道如何直接执行此操作,但是如果您创建一个函数并通过引用传递它将显示该行为。





//在主要或其他什么

//创建对象A

对象A =新对象();

someFunction(A);







private void someFunction(ref o B)B)

{

对象C =新对象();

B = C;

}



现在A已经改为C到B


不确定这对你有帮助,我不得不承认我没有'我想出了一个很好的理由直接在C#中做到这一点,虽然我似乎记得在我的旧C天中使用它多一次


-mwalts

Hmm, don''t know how to do this directly, however if you create a function and pass it by reference it will exhibit that behaviour.

i.e

//in main or whatever
//create object A
object A = new object();
someFunction(A);
.
.
.
private void someFunction(ref object B)
{
object C = new object();
B = C;
}


Now A has been changed to C through B

Not sure if that helps you, I have to admit I haven''t come up with a good reason to do it directly in C# yet, though I seem to remember using it more then once in my old C days

-mwalts



嗯,不知道如何直接执行此操作,但是如果您创建一个函数并通过引用传递它,它将表现出这种行为。 />




//在main或者其他

//创建对象A

对象A =新对象();

someFunction(A);







private void someFunction(ref对象B)

{

对象C =新对象();

B = C ;

}


现在A已经改为C到B


不确定这对你是否有帮助,我不得不承认我没有提出一个很好的理由直接在C#中做到这一点,尽管我似乎记得在我的旧C天中使用它的时间超过一次/>

-mwalts
Hmm, don''t know how to do this directly, however if you create a function and pass it by reference it will exhibit that behaviour.

i.e

//in main or whatever
//create object A
object A = new object();
someFunction(A);
.
.
.
private void someFunction(ref object B)
{
object C = new object();
B = C;
}


Now A has been changed to C through B

Not sure if that helps you, I have to admit I haven''t come up with a good reason to do it directly in C# yet, though I seem to remember using it more then once in my old C days

-mwalts



好​​吧,现在A已被设置为C的内容,但是更改A也不会改变C.它们不会占用相同的内存,它们仍然只是彼此的副本。


如果你想做内存操作,你必须跳转到非托管代码(不安全的代码) ,检查它上面的msdn),这真的很快就会变得非常讨厌。

Well, now A has been set to the contents of C, but changing A would not also change C. They don''t occupy the same memory, they are still just copies of one another.

If you want to do memory manipulation you have to make the jump into unmanaged code (unsafe code, check msdn on it) and that gets real nasty real fast.


这篇关于C#对象参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆