你如何在另外两个对象之间共享同一个对象?例如,我喜欢那种味道:
class A
{
private string foo_; // It could be any other class/struct too (Vector3, Matrix...)
public A (string shared)
{
this.foo_ = shared;
}
public void Bar()
{
this.foo_ = "changed";
}
}
...
// inside main
string str = "test";
A a = new A(str);
Console.WriteLine(str); // "test"
a.Bar();
Console.WriteLine(str); // I get "test" instead of "changed"... :(
Run Code Online (Sandbox Code Playgroud)
在这里,我不想给出Bar方法的参考.我想要实现的是在C++中看起来像这样的东西:
class A
{
int* i;
public:
A(int* val);
};
A::A (int* val)
{
this->i = val;
}
Run Code Online (Sandbox Code Playgroud)
我读到有一些参考/出口的东西,但我无法得到我在这里要求的东西.我只能在我使用ref/out参数的方法范围中应用一些更改...我还读过我们可以使用指针,但是没有其他方法可以做到吗?
c# ×1