I want to create an error message string. It should contain multiple hints to fix the errors.
First I created something like this
string errorMessage = string.Empty;
if (1 == 1)
errorMessage += "- hint 1\n";
if (2 == 2)
errorMessage += "- hint 2\n";
if (3 == 3)
errorMessage += "- hint 3";
// do something with errorMessage
And I thought about cleaning it up. I created an extension method
public static void AppendIf(this string s, bool condition, string txtToAppend)
{
if (condition)
s += txtToAppend;
}
And call it within my class
string errorMessage = string.Empty;
errorMessage.AppendIf(1 == 1, "- hint 1\n");
errorMessage.AppendIf(2 == 2, "- hint 2\n");
errorMessage.AppendIf(3 == 3, "- hint 3");
// do something with errorMessage
But errorMessage
stays empty. I thought this
acts like the ref
keyword so what is wrong with my extension method?