简体   繁体   English

定义具有大量属性的默认类属性的最佳方法

[英]best way to define default class property having large number of properties

What is the best to define default class properties when you have large number of class properties and the default value of ever property is same.Can we do looping to set the default value in such cases. 当您具有大量的类属性并且ever属性的默认值相同时,最好定义默认类属性。在这种情况下,我们可以循环执行以设置默认值吗? If yes how can we do that. 如果是的话,我们该怎么做。

public class PathologyTest
{
    public string testId { get; set;}
    public string testName { get; set;}
    public string testDate { get; set;}
    public string testType { get; set;}
    ...
}

Similarly there are around 150 properties of the class and the default value of every property is "invalid patient". 同样,该类大约有150个属性,每个属性的默认值是“无效患者”。

Currently I am doing [System.ComponentModel.DefaultValue("invalid patient")] for each property. 目前,我正在为每个属性执行[System.ComponentModel.DefaultValue("invalid patient")] Is there any way of looping to set the default value. 是否有任何循环方法来设置默认值。 Please suggest what is the best way? 请提出最好的方法是什么?

  • C# 5 and earlier : You have to set them in constructor. C#5和更早版本 :您必须在构造函数中进行设置。
  • C# 6 : You can set default value like public int X { get; set; } = x; C#6 :您可以设置默认值,例如public int X { get; set; } = x; public int X { get; set; } = x; ( Languages features in C# 6 and VB 14 ) C#6和VB 14中的语言功能

So 150 properties 共有150个住宿
Someone can look at the code and know what it is doing 有人可以看一下代码,知道它在做什么

private string defaultValue = "default value"
private string testID = defaultValue;
public string TestId 
{ 
    get
    {
        return testID;
    }
    set 
    {
        testID = value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM