I recently came across this bit of code:
public void AppendTextColour(string text, Color color, bool AddNewLine = false)
{
rtbDisplay.SuspendLayout();
rtbDisplay.SelectionColor = color;
rtbDisplay.AppendText(AddNewLine
? $"{text}{Environment.NewLine}"
: text);
rtbDisplay.ScrollToCaret();
rtbDisplay.ResumeLayout();
}
What fascinates me about it is the restructuring of AppendText();
bit here:
rtbDisplay.AppendText(AddNewLine? $"{text}{Environment.NewLine}": text);
I can only guess that the question mark is used to instantiate the boolean value, however the dollar sign and the double dots sign here are absolutely obscure. Can anyone dissect this bit and explain it to me?
I apologise if I am being a bit ambiguous but I was unable to find any relevant information anywhere. :/
This line:
rtbDisplay.AppendText(AddNewLine? $"{text}{Environment.NewLine}": text);
can be written as:
if (AddNewLine){
rtbDisplay.AppendText(string.Format("{0}{1}",text, Environment.NewLine)); // or just (text + Environment.NewLine)
} else {
rtbDisplay.AppendText(text);
}
It uses the ternary operator and string interpolation (introduced in C# 6)