.NET Properties

本文关键字:Properties NET | 更新日期: 2023-09-27 18:08:09

有人能帮我弄清楚如何实现以下目标吗:

PersonalInfo info = new PersonalInfo();
info.Contact.Name = "name";
info.Contact.Telephone = "2323232";

谢谢。


实现了以下目标。

PersonalInfo info = new PersonalInfo(); 
info.Contact.Name = "name"; 
info.Contact.Telephone = "2323232";

但在以下情况下,我该怎么办?

PersonalInfo info = new PersonalInfo(); 
info.Contact.Name = "name"; 
info.Contact.Telephone = "2323232";
info.Contact.Office.Address = "Sweden"

谢谢你的帮助。

.NET Properties

public class Contact{
  public string Name {get;set;}
  public string Telephone {get;set;}
}
public class PersonalInfo{
  public Contact Contact {get;set;}
  public PersonalInfo(){
    this.Contact = new Contact();
  }
}
var info = new PersonalInfo();
info.Contact.Name = "name";
info.Contact.Telephone = "2323232";

;(

private void Test()
{
    PersonalInfo pi = new PersonalInfo();
    pi.Contact = new Contact();
    pi.Contact.Name = "test";
}
public class Contact
{
    public string Name {get;set;}
    public string Telephone {get;set}
}
public class PersonalInfo
{
    public Contact Contact {get;set;}
}

如果您想自动实例化联系人详细信息,请将此构造函数添加到personalInfo类:

    public PersonalInfo()
    {
        this.Contact = new Contact();
    }
public class PersonalInfo
{
    public Contact contact = new Contact(); 
}
public class Contact
{
    public string Name { get; set; }
    public string Telephone { get; set; }
}