【发布时间】:2019-08-11 04:30:28
【问题描述】:
如果两个类具有相同的键(它只是一个字符串),我希望它们共享一个对象,因此如果对象的值发生变化,它会在两个类中发生变化。我认为这就像让两个对象指向相同的内存地址一样,所以在 C 中可以用指针来完成。为了阐明我想要做什么,我准备了下一个示例代码:
class MyObject
{
string key;
int value;
public MyObject(string key, int value)
{
this.key = key;
this.value = value;
}
}
class MainClass{
MyObject parameter = new MyObject("key", 5);
List<MyObject> list = new List<MyObject>();
list.Add(parameter);
}
class SecondaryClass{
MyObject parameter = new MyObject("key", 0);
List<MyObject> list = new List<MyObject>();
list.Add(parameter);
}
MainClass mainClass = new MainClass();
SecondaryClass secondaryClass = new SecondaryClass();
foreach (MyObject newParameter in mainClass.list)
{
// Try to find a match in the parameterKey
MyObject parameter = secondaryClass.list.Find(p => p.Key == newParameter.Key);
// If there is a match, update the object
if (parameter != null)
{
parameter = newParameter;
}
}
Console.WriteLine(mainClass.parameter.value) // This output 5
Console.WriteLine(secondaryClass.parameter.value) // This output 0
看到secondaryClass的参数还是指向0,值没有更新。可以在 C# 中执行此操作吗?也许分享参考?
--- 编辑 ---
我现在按照 Jon Skeet 的要求提供了一个最小、完整和可验证的示例。
class MyObject
{
public string key;
public int value;
public MyObject(string key, int value)
{
this.key = key;
this.value = value;
}
}
class MainClass
{
public MyObject parameter = new MyObject("key", 5);
public List<MyObject> list = new List<MyObject>();
public MainClass()
{
list.Add(parameter);
}
}
class SecondaryClass
{
public MyObject parameter = new MyObject("key", 0);
public List<MyObject> list = new List<MyObject>();
public SecondaryClass()
{
list.Add(parameter);
}
}
class Program
{
static void Main(string[] args)
{
MainClass mainClass = new MainClass();
SecondaryClass secondaryClass = new SecondaryClass();
foreach (MyObject newParameter in mainClass.list)
{
// Try to find a match in the parameterKey
MyObject parameter = secondaryClass.list.Find(p => p.key == newParameter.key);
// If there is a match, update the object
if (parameter != null)
{
parameter = newParameter;
}
}
Console.WriteLine(mainClass.parameter.value); // This output 5
Console.WriteLine(secondaryClass.parameter.value); // This output 0, I expected 5
mainClass.parameter.value = 7;
Console.WriteLine(mainClass.parameter.value); // This output 7
Console.WriteLine(secondaryClass.parameter.value); // This output 0, I expected 7
}
}
如您所见,对象secondaryClass.parameter 没有更改,仍然指向原始对象。当然,如果只有一个对象,我可以在末尾添加这两行:
secondaryClass.parameter = mainClass.parameter;
mainClass.parameter.value = 9;
Console.WriteLine(mainClass.parameter.value); // This output 9
Console.WriteLine(secondaryClass.parameter.value); // This output 9
主要有两个问题:
MyObject 比此处显示的更复杂,包含更多 里面的属性。
MainClass 和 SecondaryClass 有多个对象 MyObject,只有具有相同键的对象才需要共享(并且两个类都可以不共享 MyObject 对象)。
【问题讨论】:
-
只是快速阅读,这不是答案,但仅供参考,所有对象(类)变量都持有对 C# 中对象的 reference。
var a = new Something(); var b = a; // now b refers to the *exact same object as a* in memory(如果Something是一个类)。顺便说一句,类也被称为引用类型,原因相同。 -
@Pac0 是的,我预料到了,但事实并非如此。
-
很难准确理解您要查找的内容,因为您只发布了伪代码。请提供minimal reproducible example。
-
使用字典太容易了。
-
@JonSkeet 完成了,下次我会从头开始这样做
标签: c# .net pass-by-reference