c# string formatting

前端 未结 13 1535
离开以前
离开以前 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:34

    You picked too simple of an example.

    String formatting:

    • allows you to use the same variable multiple times: ("{0} + {0} = {1}", x, 2*x)
    • automatically calls ToString on its arguments: ("{0}: {1}", someKeyObj, someValueObj)
    • allows you to specify formatting: ("The value will be {0:3N} (or {1:P}) on {2:MMMM yyyy gg}", x, y, theDate)
    • allows you to set padding easily: (">{0,3}<", "hi"); // ">hi <"

提交回复
热议问题