<分区>
我在谷歌上搜索最佳 C# 字符串连接,我在 Microsoft C# 代码约定中找到了这个: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions
Use the
+
operator to concatenate short strings, as shown in the following code:
string displayName = nameList[n].LastName + ", " + nameList[n].FirstName;
但我真的更喜欢 (String.Format
) "$
"以提高可读性恕我直言:
string firstName = "MyName";
string lastName = "MyLastName";
string displayName = $"{lastName}, {firstName}";
Console.WriteLine(displayName); // "MyLastName, MyName"
你能告诉我你认为哪一个更好吗?还是只是个人喜好?
谢谢!