Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将值设置为对象内的字段;只有";如果C中的值不为null#_C#_Object_Null - Fatal编程技术网

C# 将值设置为对象内的字段;只有";如果C中的值不为null#

C# 将值设置为对象内的字段;只有";如果C中的值不为null#,c#,object,null,C#,Object,Null,我是C#的新手。我可以在以下方面得到你的帮助。 我有以下代码 private void foo(TropicalRequest tropicalRequest) { var buildRequest = new RestRequest() { BaseUrl = tropicalRequest.baseUrl, StatusCode = tropicalRequest.statusCode, InitialDate = tro

我是C#的新手。我可以在以下方面得到你的帮助。 我有以下代码

    private void foo(TropicalRequest tropicalRequest)
    {
    var buildRequest = new RestRequest()
     {
      BaseUrl = tropicalRequest.baseUrl,
      StatusCode = tropicalRequest.statusCode,
      InitialDate = tropicalRequest.createdDate.Value
     };
    //Code call to save into DB
    }
“tropicalRequest.createdDate.Value”字段不包含每个场景的值,当它为null时,我的代码中断。我已经写了下面的代码,但我想优化它,非常感谢您的帮助

    private void foo(TropicalRequest tropicalRequest)
    {
    var buildRequest = new RestRequest()
     {
      BaseUrl = tropicalRequest.baseUrl,
      StatusCode = tropicalRequest.statusCode
     };

    if(tropicalRequest.createdDate.HasValue)
      buildRequest.InitialDate = tropicalRequest.CreatedDate.Value;
    //Code call to save into DB
    }
基本上,我只想在值不为null的情况下将值设置为对象内部的字段

编辑#1:InitialDate和CreatedDate都是数据类型DateTimeOffset


编辑#2:InitialDate不可为null,CreatedDate可为null DateTimeOffset。

您可以使用null propogation运算符:

var buildRequest = new RestRequest()
 {
  BaseUrl = tropicalRequest.baseUrl,
  StatusCode = tropicalRequest.statusCode,
  InitialDate = tropicalRequest.createdDate ?? default(DateTimeOffset)
 };

一个重要的注意事项是,DateTime对象的默认值是它的
MinValue
,这意味着如果您没有分配任何内容,它的值将是
01/01/0001 00:00
。考虑<代码> TrpCalApple。CreatedDate < /代码>是第二个示例中的代码> null <代码>,然后<代码> BudiRestQuest.IrralDATE < /C> >将是最小值。

如果希望它为
null
,则表示它应为
null
类型。你 可以使用以下语法来定义它们

DateTime? InitialDate;
如果在类中这样声明,则不需要检查
HasValue
属性。您可以像这样直接分配
InitialDate=tropicalRequest.createdDate

正如另一个答案所述,如果您使用的是
c#6.0
,则可以尝试使用operator。或者使用条件运算符:

var buildRequest = new RestRequest()
 {
  BaseUrl = tropicalRequest.baseUrl,
  StatusCode = tropicalRequest.statusCode,
  InitialDate = tropicalRequest.createdDate.HasValue ? tropicalRequest.CreatedDate.Value : DateTime.MinValue;
 };

编译器已经对其进行了优化。如果您只是想以不同的方式格式化代码,可以使用此语法。因此,如果存在值,则将使用CreatedDate,否则将使用在另一侧提供的值??语法,在本例中为默认值(DateTimeOffset),与不赋值相同。因此,有人可能会说,您当前的语法实际上比这个“更好”

private void foo(TropicalRequest tropicalRequest)
{
    var buildRequest = new RestRequest()
    {
        BaseUrl = tropicalRequest.baseUrl,
        StatusCode = tropicalRequest.statusCode,
        InitialDate = tropicalRequest.CreatedDate ?? default(DateTimeOffset);
    };
}

由于您的InitialDate是非空的DateTimeOffset,所以即使您未分配它,它也将始终初始化为DateTimeOffset的默认值。

当前代码有什么问题?如果
createdDate.HasValue
正确,它将为
InitialDate
设置值,是否有更好的方法或更短的方法在对象创建中写入if语句?您的属性是否都可以为null DateTimeOffset?或者InitialDate非null DateTimeOffset和TropicalRequest的CreatedDate是否可以为null?只有TropicalRequest的CreatedDate可以为null。是否确定?值语法?DateTime.Yep没有值。若值在第一种情况下为null,则InitialDate变为null,在第二种情况下为默认值(DateTime)。有关null传播运算符的一些信息。已尝试此操作,但无法工作,因为该字段不可为null。当我使用默认值(DatetimeOffset)时,我得到的错误是“?操作数不能应用于DatetimeOffset的操作数。”但DateTime上没有Value属性。你用的是什么?。用于访问包装在可空对象中的类型的属性的符号。例如date?.Year。它是DateTimeOffset数据类型。我已经更新了问题中的相同内容。这种方法将设置一个最小值,如果该值为空,我的要求是不使用该字段。@ndavid9:
DateTime。MinValue
是DateTime对象的默认值,如果希望它为null,则意味着您必须将其设置为可为null的DateTime对象。是的,我理解,但在我的场景中,InitialDate不可为null,CreateDate可为null。它也是DateTimeOffset类型,而不是dateTime“DateTimeOffset?CreatedDate{get;set;}”
DateTimeOffset
可以为空。我在tropicalRequest.CreatedDate.Value上尝试了相同的方法??默认值(DateTimeOffset);这就是它不起作用的原因。再次感谢:)