C#-如何将List设置为至少有一个元素

本文关键字:有一个 元素 设置 List C#- | 更新日期: 2023-09-27 18:26:07

我有以下问题:

我有一个名为MedicalInfo的类,我希望它的列表至少有一个Prescription。问题是,我想在它的构造函数中加入一些逻辑,如下所示,但它有一个主要缺点:当我反序列化它(xml)时,它会添加一个新的。

我想象序列化程序首先创建对象(并因此添加ALWAYS一个Prescription实例),然后添加字段的值。所以,简而言之,每次我反序列化时,我都会得到一个额外的实例。。。我该如何避免这种情况?

为了说明一些上下文,我正在开发一个WinForms应用程序,该应用程序在应用程序关闭时进行序列化,在应用程序打开时进行反序列化,以便在使用过程中使数据可用。此外,MedicalInfo是名为Client的类的一部分,该类具有许多属性,例如策略列表。我序列化和反序列化的是一个客户端列表(地址簿)。我想在反序列化后做"检查"。。。但在某些情况下,这需要双前臂。我不确定这是否是最佳的。

public class MedicalInfo
{
    public string MedicareNumber { get; set; }
    public DateTime PartAEffectiveDate { get; set; }
    public DateTime PartBEffectiveDate { get; set; }
    public List<Prescription> Prescriptions { get; set; }
    public MedicalInfo()
    {
        PartAEffectiveDate = new DateTime(1900, 01, 01);
        PartBEffectiveDate = new DateTime(1900, 01, 01);
        Prescriptions = new List<Prescription>().Min(1);
        if (Prescriptions.Count == 0)
        {
            Prescriptions.Add(new Prescription());
            Prescriptions.FirstOrDefault().Instructions = "Default Prescription";
        }
    }
}
public class Client
{
    #region Properties
    private Guid ID { get; set; }
    [XmlIgnore]
    public string FullName
    {
        get
        {
            return FirstName + " " + MiddleName + " " + LastName;
        }
    }
    [XmlIgnore]
    public string DisplayName
    {
        get
        {
            return LastName + ", " + FirstName;
        }
    }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public bool Male { get; set; }
    public DateTime Birthday { get; set; }
    public Address Address { get; set; }
    public int SSN { get; set; }
    public long Phone { get; set; }
    public long AlternatePhone { get; set; }
    public string Email { get; set; }
    public int Height { get; set; }
    public int Weight { get; set; }
    public string Notes { get; set; }
    public BankAccount BankInfo { get; set; }
    public MedicalInfo MedInfo { get; set; }
    public DateTime RegisteredTime { get; set; }
    #endregion Properties
    public Client()
    {
        ID = new Guid();
        RegisteredTime = DateTime.Now;
        Birthday = new DateTime(1900, 01, 01);
        BankInfo = new BankAccount();
        MedInfo = new MedicalInfo();
        Address = new Address();
    }
}
public class InsuranceClient : Client
{
    public bool Tobacco { get; set; }
    public PolicyCollection Policies { get; set; }
    public InsuranceClient() : base()
    {
        Policies = new PolicyCollection();
        if (Policies.Count == 0)
        {
            Policies.Add(new Policy());
            Policies.FirstOrDefault().Plan = "Default Policy";
        }
    }
}

C#-如何将List设置为至少有一个元素

您需要从构造函数中删除初始化。我建议使用空合并运算符??,如下所述。

public class MedicalInfo
{
    public string MedicareNumber { get; set; }
    public DateTime PartAEffectiveDate { get; set; }
    public DateTime PartBEffectiveDate { get; set; }
    public List<Prescription> Prescriptions
    {
        get
        {
            return _prescritpions ?? (_prescritpions = GetDefaultPrescriptions());
        }
        set
        {
            _prescritpions = value;
        }
    }
    List<Prescription> _prescritpions;
    public MedicalInfo()
    {
        PartAEffectiveDate = new DateTime(1900, 01, 01);
        PartBEffectiveDate = new DateTime(1900, 01, 01);
    }
    static List<Prescription> GetDefaultPrescriptions()
    {
        return new List<Prescription> 
               { 
                   new Prescription { Instructions = "Default Description" }     
               };
    }
}

其优点是,如果_perscriptions变量已经实例化,那么它将简单地返回。否则,如果它是null,它将使用GetDefaultPrescriptions函数中指定的默认实例来实例化它。这应该可以防止在序列化问题中重复创建默认变量。

添加一个成员变量来存储处方。将业务规则添加到处方属性的getter中。

public class MedicalInfo
{
    public string MedicareNumber { get; set; }
    public DateTime PartAEffectiveDate { get; set; }
    public DateTime PartBEffectiveDate { get; set; }
    private List<Prescription> _prescritpions;
    public List<Prescription> Prescriptions
    {
        get
        {
            if (_prescritpions.Count == 0)
            {
                _prescritpions.Add(new Prescription
                {
                    Instructions = "Default Description"
                });
            }
            return _prescritpions;
        }
        set
        {
            _prescritpions = value;
        }
    }
    public MedicalInfo()
    {
        PartAEffectiveDate = new DateTime(1900, 01, 01);
        PartBEffectiveDate = new DateTime(1900, 01, 01);
        _prescritpions = new List<Prescription>();
    }
}

edit:从构造函数中删除了未成功的处方实例化。