Reputation: 13
I am new to C#. I can use your help with the below. I have the following code.
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
}
The "tropicalRequest.createdDate.Value" field does not contain value for every scenario, when it is null my code breaks. I have written the below code but I want to optimize it, your help is much appreciated.
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
}
Basically, I want to set the value to the field inside the object only if the value is not null.
Edit #1: InitialDate and CreatedDate are both of the DataType DateTimeOffset.
Edit #2: InitialDate is not nullable and CreatedDate is nullable DateTimeOffset.
Upvotes: 0
Views: 1045
Reputation: 558
You can use null propogation operator:
var buildRequest = new RestRequest()
{
BaseUrl = tropicalRequest.baseUrl,
StatusCode = tropicalRequest.statusCode,
InitialDate = tropicalRequest.createdDate ?? default(DateTimeOffset)
};
Upvotes: 1
Reputation: 5121
It will already be optimized by the compiler. If you just want to format the code differently you can use this syntax. So the CreatedDate will be used if there is a value and otherwise the value provided on the other side of the ?? syntax, in this case default(DateTimeOffset) which will be the same as not assigning. So one could argue your current syntax is actually "better" than this.
private void foo(TropicalRequest tropicalRequest)
{
var buildRequest = new RestRequest()
{
BaseUrl = tropicalRequest.baseUrl,
StatusCode = tropicalRequest.statusCode,
InitialDate = tropicalRequest.CreatedDate ?? default(DateTimeOffset);
};
}
As your InitialDate is non null DateTimeOffset it will be always initialized to Default value of the DateTimeOffset even if you do not assign it.
Upvotes: 0
Reputation: 29026
An important note, the Default value for the DateTime object is its MinValue
which means if you have not assign anything means its value will be 01/01/0001 00:00:00
. Consider tropicalRequest.CreatedDate
is null
in the second example then buildRequest.InitialDate
will be the minimum value.
If you want it to be
null
means it should be ofNullable
type. you can use the following syntax for defining them.DateTime? InitialDate;
If you declared like this in the class, then you need not to check with HasValue
property. you can directly assign like this InitialDate = tropicalRequest.createdDate
As the other answer stated, you can try Null-conditional Operators operator If you are using c# 6.0
. or else Make use of conditional operator :
var buildRequest = new RestRequest()
{
BaseUrl = tropicalRequest.baseUrl,
StatusCode = tropicalRequest.statusCode,
InitialDate = tropicalRequest.createdDate.HasValue ? tropicalRequest.CreatedDate.Value : DateTime.MinValue;
};
Upvotes: 0