c# string formatting

前端 未结 13 1518
离开以前
离开以前 2020-12-25 14:12

I m curious why would i use string formatting while i can use concatenation such as

Console.WriteLine(\"Hello {0} !\", name);

Console.WriteLine(\"Hello \"+          


        
相关标签:
13条回答
  • 2020-12-25 14:50

    The first format is recommended. It allows you to specify specific formats for other types, like displaying a hex value, or displaying some specific string format.

    e.g.

    string displayInHex = String.Format("{0,10:X}", value); // to display in hex
    

    It is also more consistent. You can use the same convention to display your Debug statement. e.g.

    Debug.WriteLine (String.Format("{0,10:X}", value));
    

    Last but not least, it helps in the localisation of your program.

    0 讨论(0)
提交回复
热议问题