How would this code be translated into C#, specifically how would sprintf be implemented in C#?
string output = "The user %s logged in";
string loggedIn = "is";
string loggedOut = "isn't";
if (TheUser.CheckStatus())
{
output = sprintf(output, loggedIn);
}
else
{
output = sprintf(output, loggedOut);
}
return output;
I’m expecting to see "The user isn't logged in" if TheUser.CheckStatus() is false.
Check out string.Format and here is a version of your code using it:
Or more simply: (using a ternary expression)