2

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?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
mbrc
  • 3,523
  • 13
  • 40
  • 64

7 Answers7

4

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;
}
Tony Bao
  • 922
  • 2
  • 12
  • 23
2

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;
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
2

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;
}
Cyral
  • 13,999
  • 6
  • 50
  • 90
  • 1
    the first code will be compiled like the second code. i just thought its good to mention it. they will compile to same thing while the first code is more readable IMO – M.kazem Akhgary Jan 01 '16 at 18:31
2

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.

Michael Petito
  • 12,891
  • 4
  • 40
  • 54
1

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);
}
senzacionale
  • 20,448
  • 67
  • 204
  • 316
  • This assumes that an `Id` value of 0 is invalid. Your `GetOrDefault` method returns 0 for a null `obj`, which could be equal to the `CurrentAthlete.Id` otherwise. – Michael Petito Jan 01 '16 at 20:45
  • @mbrc it has a subtle difference, in that the result is zero instead of null when `LeaderMembership` is null. If `CurrentAthlete.Id` can be zero, then you're testing `0 != 0` which is false, instead of `null != 0` which is true. – Michael Petito Jan 02 '16 at 15:09
  • @Michael do you have any suggestion how to make method better? It must work correctly. – mbrc Jan 02 '16 at 16:26
0

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.

  • compiler will optimize to read the property only once anyways. keep your code clean! – SimpleVar Jan 01 '16 at 18:48
  • @SimpleVar No, the compiler won't do that. The runtime might, depending on what the property getter looks like, but if the property getter has a side effect, you will easily be able to determine that that side effect takes place twice if you read the property twice. Anyway, the question was how to rewrite specific C# 6 code in C# <6. Regardless of whether reading the property twice is acceptable, it would be a change in behaviour, it would not be a faithful rewrite to a previous language version. –  Jan 01 '16 at 18:58
0

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);
EgoPingvina
  • 734
  • 1
  • 10
  • 33