c# string formatting

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

    In addition to reasons like Ignacio's, many (including me) find String.Format-based code much easier to read and alter.

    string x = String.Format(
      "This file was last modified on {0} at {1} by {2}, and was last backed up {3}."
      date, time, user, backupDate);
    

    vs.

    string x = "This file was last modified on " + date + " at "
    + time + " by " + user + " and was last backed up " + backupDate + ".";
    

提交回复
热议问题