xxxxxxxxxx
//Return stirng representation of nullable DateTime
DateTime? x = null;
return x.HasValue == true ? x.Value.ToString() : "No Date";
xxxxxxxxxx
// This is used if the source of a value is uncertain to exist
// For both of these examples, if 'p' is null, 'name' and 'age' will be null
// (as opposed to throwing an error)
string name = p?.FirstName;
string age = p?.Age;
string silver = preciousMetals?[4]?.Name;
xxxxxxxxxx
variable ??= expression;
// Same as:
//if (variable is null)
//{
// variable = expression;
//}
xxxxxxxxxx
// Prior to C# 6
var title = null;
if (post != null)
title = post.Title;
// c#6
var title = post?.Title;
// Prior to C# 6
var count = 0;
if (post != null)
{
if (posts.Tags != null)
{
count = post.Tags.Count;
}
}
// C# 6
var count = post?.Tags?.Count;