【问题标题】:How to Remove Default Property Values from Class如何从类中删除默认属性值
【发布时间】:2022-01-20 08:23:55
【问题描述】:

在一个类中,我有一组属性,我将能够为某些属性分配值,而某些属性具有默认值。现在我想在返回对象响应时删除那些默认值属性。

例子:

Public class Root {
public int intValue{get;set;}
public string strName{get;set;}
public string imgUrl{get;set}
public DateTime dtmCreated{get;set;}
public DateTime dtmExpiry{get;set;}
}

当我从上面检索响应时,我看到 imgurl 为空,并且默认为到期日期。我不想在某些情况下展示这些。如何在显示 api 响应时从对象中删除这些属性。

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-web-api webapi


    【解决方案1】:

    intDateTime 不能“没有值”。但是它们的可为空的对应物可以。如果您希望属性允许空值,请将它们设为可为空。例如:

    public int? intValue { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      相关资源
      最近更新 更多