How to implement this in C# 4 with extensions:
string challengeId;
if(challengeId == mViewModel?.Challenge?.Id)
where Challenge.Id is string
I was given some extension:
public static TValue GetOrDefault<TObject, TValue>(this TObject obj, Func<TObject, TValue> getter, TValue defaultValue = default(TValue))
where TObject : class
{
return obj == null ? defaultValue : getter(obj);
}
then i can use it so:
string challengeId;
if(challengeId == mViewModel.GetOrDefault(x => x.Challeng).GetOrDefault(x => x.Id)
will be this ok in all combinations. What about if Id's are not string
and we want to compare int
?
int challengeId;
if(challengeId == mViewModel.GetOrDefault(x => x.Challeng).GetOrDefault(x => x.Id)
where Challenge.Id is int