C# 如何使两个类属性引用相同的值

C# 如何使两个类属性引用相同的值,c#,C#,我正在努力做到以下几点: 我想要两个类Class01和Class02。Class02Integer的属性用Class01的我的属性Integer初始化。当我更改我的Class01时。整数现在我想要我的Class02。整数也发生了变化。我该怎么做 Class01 one = new Class01 { Integer = 16 }; Class02 two = new Class02 { Integer = one.Integer }; Console.WriteLine("Class one:

我正在努力做到以下几点:

我想要两个类
Class01
Class02
Class02
Integer
的属性用
Class01
的我的属性
Integer
初始化。当我更改我的
Class01
时。整数现在我想要我的
Class02
。整数也发生了变化。我该怎么做

Class01 one = new Class01 { Integer = 16 };
Class02 two = new Class02 { Integer = one.Integer };

Console.WriteLine("Class one: {0} -- Class two: {1}", one.Integer, two.Integer); 
// Prints: Class one: 16 -- Class two: 16

one.Integer++;

Console.WriteLine ("Class one: {0} -- Class two: {1}", one.Integer, two.Integer); 
// Prints:             Class one: 17 -- Class two: 16
// I want it to print: Class one: 17 -- Class two: 17

你不能这样做,因为
int
是值类型,每个类都有自己的int值副本


您可以编写包含
int
值的类包装器,并在类中设置对它的引用,或者您也可以尝试使用box
int

来区分引用类型和值类型

更改引用类型的值时,引用该对象的所有位置都会更改该值

int
是一种值类型,因此在一个对象中更改integer属性时,它不会在另一个对象中自动更新该属性

Class01
是一种引用类型,如果您以与
Integer
属性类似的方式使用它,它将按照您希望的方式工作

值类型的示例:int、double、bool、char、DateTime

引用类型示例:表单、字符串、列表


最糟糕的解决方案是使用一个带有
static
变量的类,例如带有
static int
的Class03,Class01和Class02都可以访问和更新它们自己的属性,但我不建议这样做。根据体系结构的不同,您可能会发现您可能希望为此值编写一个包装器,使其成为引用类型,或者使用事件通知其他对象有关更改的信息,或者如果类非常关联,则使用某种类型的继承。

我知道有一种方法,这可能不是最简单的方法,但它确实有效:

您可以创建一个自定义事件处理程序,在初始化时在事件处理程序中订阅这些类,一旦在事件处理程序中订阅了这些类,就为这些类创建一个公共void,将该公共void称为public void UpdateIntegerValue(int),在该事件处理程序中,使用一个参数超出整数值,另一个参数超出类对象本身来引发该事件,然后只需为每个类实例设置值

最后,只需一行代码就可以轻松地更新所有这两个类,比如UpdateIntegerValueForAllClasses(objectownerClass,int integerToUpdate)

如果您没有完全理解或需要更多详细信息,请告诉我。

@JRace

Class01
对象赋值只会复制该值,而不会复制内存引用。 因此,每当您在
one.Integer
中进行更改时,它将永远不会反映到
two.Integer
。 您可以创建一个具有int属性的新类(引用类型),创建该类的实例并进行工作。 这会有帮助的

public class Class02
    {
        public Data Integer { get; set; }
    }
    public class Class01
    {
        public Data Integer { get; set; }
    }
    public class Data {

        public int Integer { get; set; }
    }
    public class A
    {
        private void myFunc()
        {
            Data data = new Data();
            data.Integer = 16;
            Class01 one = new Class01 { Integer = data };
            Class02 two = new Class02 { Integer = data };
            Console.WriteLine("Class one: {0} -- Class two: {1}", one.Integer.Integer, two.Integer.Integer);
            // Prints: Class one: 16 -- Class two: 16
            data.Integer++;
            Console.WriteLine("Class one: {0} -- Class two: {1}", one.Integer.Integer, two.Integer.Integer);
            // Prints:             Class one: 17 -- Class two: 16
            // I want it to print: Class one: 17 -- Class two: 17
        }
    }

如果希望在类的所有实例之间共享变量,则可以创建一个父类,这两个类都将从中继承,并在其中定义一个受保护的静态变量(
protected
=仅可对此类及其继承的类访问;
static
=跨类型共享):


你能修改
Class01
Class02
吗?谢谢你的快速回答!我不知道这些不同的..errm.类型。因此,我不把整数保存为int,而是在Class02中保留我的Class01属性,并可以使用class2.class1调用整数值。Integer@JRace不客气。如果我理解正确,你可以n在Class02中声明
内部静态int Integer
,因此在Class01中您可以将其称为
Class02.Integer
,根本不需要实例化Class02对象。在执行此操作时请小心,这样它就不会
null
abstract class ClassAbstract{
    protected static int _myInteger {get;set;}
}

class Class01:ClassAbstract{
    public int MyInteger {
        get{return _myInteger;}
        set{ _myInteger=value;}
    }
}

class Class02:ClassAbstract{
    public int MyInteger {
        get{return _myInteger;}
        set{ _myInteger=value;}
    }
}

static class Program {
    public void Main(){
        Class01 one = new Class01();
        one.MyInteger = 16;
        Class02 two = new Class02();
        Console.WriteLine("Class two: {0}", two.MyInteger);
    }
}