C# 如何在两个类之间共享变量?

C# 如何在两个类之间共享变量?,c#,C#,您将如何在其他两个对象之间共享同一个对象?例如,我想要那种味道的: 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_ = "change

您将如何在其他两个对象之间共享同一个对象?例如,我想要那种味道的:

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"... :(
在这里,我不想引用Bar方法。我想要实现的是在C++中看起来像这样的东西:

class A
{
  int* i;
public:
  A(int* val);
};

A::A (int* val)
{
  this->i = val;
}
我读到有一些参考/输出的东西,但我不能得到我在这里要求的东西。我只能在使用ref/out参数的方法范围内应用一些更改。。。
我还读到我们可以使用指针,但是没有其他方法可以做到吗?

这与共享对象无关。您将对字符串的引用传递到构造函数中。该引用已复制到私有成员
foo\ucode>。后来,您调用了
B()
,它将
foo\uu
更改为“changed”

您从未修改过
str
str
main
中的局部变量。你从未传递过对它的引用

如果您想更改
str
,您可以将B定义为

   public void Bar(ref string s)
   {
     this.foo_ = "changed";
     s = this.foo_;
   }

考虑:

public class C
{
    public int Property {get;set;}
}

public class A
{
    private C _c;
    public A(C c){_c = c;}

    public void ChangeC(int n) {_c.Property = n;}
}

public class B
{
    private C _c;
    public B(C c){_c = c;}

    public void ChangeC(int n) {_c.Property = n;}
}
大体上:

C myC = new C() {Property = 1;}
A myA = new A(myC);
B myB = new B(myC);

int i1 = myC.Property; // 1
myA.ChangeC(2);
int i2 = myC.Property; // 2
myB.ChangeC(3);
int i3 = myC.Property; // 3

我将我的答案分成两部分: 1) 如果变量是引用类型,则由于将其引用传递给所有感兴趣的对象,因此该变量已被共享。唯一需要注意的是引用类型实例是可变的。 2) 如果变量是值类型,则必须使用ref或out或一些可变的包装器,并且可以使用方法或属性更改包装器内的值。
希望这有帮助。

将字符串包装在类中。您需要这样做,因为字符串是不可变的。任何更改字符串的尝试实际上都会产生一个新字符串

class Foo {
    class StringHolder {
        public string Value { get; set; }
    }
    private StringHolder holder = new StringHolder();
    public string Value  {
       get { return holder.Value; }
       set { holder.Value = value; }
    }
    public Foo() { }
    // this constructor creates a "linked" Foo
    public Foo(Foo other) { this.holder = other.holder; } 
}

// .. later ...

Foo a = new Foo { Value = "moose" };
Foo b = new Foo(a); // link b to a
b.Value = "elk"; 
// now a.Value also == "elk"
a.Value = "deer";
// now b.Value also == "deer"

您需要将参数作为方法的引用传递

    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(ref string myString)
            {
               myString = "changed";
            }
        }

static void Main()
        {
            string str = "test";
            A a = new A(str);

            Console.WriteLine(str); // "test" 
            a.Bar(ref str);
            Console.WriteLine(str);

        }

当变量是字符串时,它是一个引用。
尝试克隆字符串

特别是在您的示例中,字符串不是一个很好的示例,因为它们是不可变的。+1我可能会将
StringHolder
设置为一个私有嵌套类。实际上,它可以按照您的建议处理,但这不是我想要做的。在这里,我真的希望我的类A和B通过将其交给A和B构造函数来处理同一个对象。然后,它们将自己处理共享对象,而不必在每次方法调用时都有一个ref参数(我想保留没有参数的Bar方法)。跟随其对象的定向边界框。如果我可以在OBB和对象之间共享位置,我就不必每次对象移动时都更新OBB位置,然后只需将对象传递给两个构造函数。这不是你在例子中所做的。另外,传递字符串也不起作用,因为字符串是不可变的。但是如果C是一个结构,那么这段代码就不能再工作了。。。你将如何解决这个问题?(例如,将Vector3传递给struct…@altefquare:你说得对。实际上,共享值类型的实例生命是没有意义的。如何共享数字1?是的,共享数字值确实没有意义,但如果您想共享一个包含该值的变量,则有意义。我想这就是为什么人们建议做一个包装器,这就是我的目的,即使我更喜欢一种更方便的方式(为每个结构创建一个类是相当痛苦的…)。谢谢你的帮助。