Is it possible to include a C# variable in a string variable without using a concatenator?

前端 未结 14 2079
遥遥无期
遥遥无期 2021-02-14 07:02

Does .NET 3.5 C# allow us to include a variable within a string variable without having to use the + concatenator (or string.Format(), for that matter).

For example (In

14条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-14 07:42

    you can use something like this as mentioned in C# documentation. string interpolation

    string name = "Horace";
    int age = 34;
    Console.WriteLine($"He asked, \"Is your name {name}?\", but didn't wait for a reply :-{{");
    Console.WriteLine($"{name} is {age} year{(age == 1 ? "" : "s")} old.");
    

提交回复
热议问题