Sunday, 14 May 2017

String Interpolation in C#

An interpolated string looks like a template string that contains interpolated expressions. An interpolated string returns a string that replaces the interpolated expressions that it contains with their string representations.The arguments of an interpolated string are easier to understand
1
2
3
public string Message { get; set; } = "Message";
//Interpolated Strings
public void WriteMessage() => Console.WriteLine($"Message is {Message} ");
we can use interpolated string anywhere we can use a string literal.
If the interpolated string contains special characters, such as the quotation mark (“), colon (:), or comma (,), they should be escaped.
1
public void WriteMessageEx1() => Console.WriteLine($"Message is \"important\" {Message} ");
if they are language elements they should be included in an expression delimited by parentheses.
1
public void WriteMessageEx2() => Console.WriteLine($"Message is {(string.IsNullOrWhiteSpace(Message) ? "empty" : "Not empty")} ");

No comments:

Post a Comment