-4

Looking for help working out what the code in return does / is called

private string MakeOrderKey(string s1, string s2)
{
   bool b = (string.Compare(s1, s2) < 0);
   return ((b ? s1 : s2) +  "/" + (b ? s2 : s1));
}
Mohammad Aghazadeh
  • 2,108
  • 3
  • 9
  • 20
Callumari
  • 5
  • 1
  • 3
  • 3
    It's called a [Ternary operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator) – Alexey S. Larionov Oct 05 '22 at 14:28
  • You should google `C# "?" operator`. That will lead you to the documentation, or at the very least the name to look up. – Logarr Oct 05 '22 at 14:28
  • @DmitryBychenko You have that backwards. It will return `s1` if `b` is true. – Logarr Oct 05 '22 at 14:29

2 Answers2

1

it's equivalent to

if (b)
{
     return s1;
}
else
{
     return s2;
}

Edit: As Alexey pointed out, I should clarify. return here isn't the return from your specific function it's just saying that the statement (b ? s1 : s2) will evaluate to either s1 or s2 based on the value of b.

Learn more here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator

frankM_DN
  • 365
  • 7
0

It's comparing the strings represented by s1 and s2 and returns a string like "bar/foo" in the comparative order of the strings.

Note that string.Compare may have issues with casing and special characters, consider using string.CompareOrdinal instead.

ThomasB
  • 161
  • 6