C# 6.0 code:
public bool HasLeaderOtherThanSelf
{
return LeaderMembership?.AthleteId != App.CurrentAthlete.Id;
}
What is correct way to implement it in C# 4?
To return null
or false
back if LeaderMembership
is null?
C# 6.0 code:
public bool HasLeaderOtherThanSelf
{
return LeaderMembership?.AthleteId != App.CurrentAthlete.Id;
}
What is correct way to implement it in C# 4?
To return null
or false
back if LeaderMembership
is null?
In C# 4.0:
public bool HasLeaderOtherThanSelf
{
return LeaderMembership != null && LeaderMembership.AthleteId != App.CurrentAthlete.Id;
}
OR
public bool? HasLeaderOtherThanSelf
{
if(LeaderMembership == null)
{
return null;
}
return LeaderMembership.AthleteId != App.CurrentAthlete.Id;
}
Like this:
public bool HasLeaderOtherThanSelf
{
if(LeaderMembership != null)
return LeaderMembership.AthleteId != App.CurrentAthlete.Id;
return false;
}
Or if you want to be able to return null
you should change your HasLeaderOtherThanSelf
type to
Nullable Bool
(bool?
):
public bool? HasLeaderOtherThanSelf
{
if(LeaderMembership != null)
return LeaderMembership.AthleteId != App.CurrentAthlete.Id;
return null;
}
You would return false
back.
public bool HasLeaderOtherThanSelf
{
if (LeaderMembership != null)
return LeaderMembership.AthleteId != App.CurrentAthlete.Id;
return false;
}
Which can be simplified to:
public bool HasLeaderOtherThanSelf
{
return LeaderMembership != null && LeaderMembership.AthleteId != App.CurrentAthlete.Id;
}
If LeaderMembership
is null, the result of LeaderMembership?.AthleteId
will be a null value of type nullable int (assuming AthleteId
is an integer).
Presumably the App.CurrentAthlete.Id
is also an integer, so the comparison of a null value of type nullable int with an int will be false.
try with 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);
}
you can use it as:
public bool HasLeaderOtherThanSelf
{
return LeaderMembership.GetOrDefault(x => x.AthleteId) != App.CurrentAthlete.Id;
}
or if you want to return null:
public static TValue? GetOrDefault<TObject, TValue>(this TObject obj, Func<TObject, TValue> getter, TValue? defaultValue = default(TValue?))
where TObject : class
where TValue : struct
{
return obj == null ? defaultValue : getter(obj);
}
Your C# 6 code translates to
public bool HasLeaderOtherThanSelf
{
var leaderMembership = LeaderMembership;
return (leaderMembership == null ? default(int?) : leaderMembership.AthleteId)
!= App.CurrentAthlete.Id;
}
Note that your code reads LeaderMembership
only once, and to accurate emulate that in C# <6, you need an additional local variable.
Note if LeaderMembership
is null
and App.CurrentAthlete.Id
is non-null, your existing code returns true
, but most of the other answers return false
for that case. false
is probably a better answer, but not an accurate translation of your existing code.
In C# 4
it look like:
public bool HasLeaderOtherThanSelf
{
return LeaderMembership != null && LeaderMembership?.AthleteId != App.CurrentAthlete.Id;
}
But use "hardcode" is not good practice) Will be better do something like this:
First variant:
public bool IsNullOrEquivalent<T>(T target, Func<T, bool> comparer)
where T : class
{
return target == null
&& comparer(target);
}
that you can use like:
var result = new NonExtensionComparer().IsNullOrEquivalent(LeaderMembership, e => e.AthleteId == App.CurrentAthlete.Id);
Second varian (extension):
public static bool IsNullOrEquivalent<T>(this T target, Func<T, bool> comparer)
{
return target == null
&& comparer(target);
}
use:
var result = LeaderMembership.IsNullOrEquivalent(e => e.AthleteId == App.CurrentAthlete.Id);