C#程序,用tostring()或不用,哪个更好

Sle*_*ght 3 .net c# string

如你所见,一个C#程序,var month定义为int,有人说没有.tostring()更好,它应该删除多余的调用,现在它是:str= "0" + Month; 但我觉得它不好.哪一个更好?为什么?谢谢!(ps:我在stackoverflow中的第一个问题)

string strM = string.Empty;
if ( Month < 10 )
    strM = "0" + Month.ToString ( );
    //strM = "0" + Month; which is better?
Run Code Online (Sandbox Code Playgroud)

Jal*_*aid 8

改为使用字符串格式:

string strM = string.Format("{0:00}", Month);
Run Code Online (Sandbox Code Playgroud)

测试:

Month: 1 => strM: "01"
Month: 12 => strM: "12"
Run Code Online (Sandbox Code Playgroud)

欲了解更多字符串格式提示检查这个.