我有一个类,看起来像这样:
public class Organization
{
public List<IJob> Jobs { get; set; }
public IJob BigBoss { get; set; }
public Organization()
{
BigBoss = new Doctor();
Jobs = new List<IJob>
{
BigBoss,
new Doctor(),
new Doctor()
};
}
}
如果我将BigBoss属性设置为一个新的值,我希望该值也能在列表中更新。当BigBoss指向新内容时,列表中的第一项也应该指向同样的内容。由于C#使用引用的方式,以下代码无法实现上述功能:
static void Main(string[] args)
{
var test = new Organization();
test.BigBoss = new Developer();
//test.Jobs[0] is still pointing to a Doctor, not a Developer
Console.WriteLine(test.Jobs[0]);
}
有没有其他更简洁的方法来做这件事?
public IJob BigBoss { get { return Jobs[0]; set { Jobs[0]=value; }
的代码。 - KV Prajapati