String output: format or concat in C#?

Question :

String output: format or concat in C#?,

Answer :

Let’s say that you want to output or concat strings. Which of the following styles do you prefer?

  • var p = new { FirstName = "Bill", LastName = "Gates" };
  • Console.WriteLine("{0} {1}", p.FirstName, p.LastName);
  • Console.WriteLine(p.FirstName + " " + p.LastName);

Do you rather use format or do you simply concat strings? What is your favorite? Is one of these hurting your eyes?

Do you have any rational arguments to use one and not the other?

I’d go for the second one.

Read More  Java Web Services API, however I can’t run a JVM on my server

,

I’m amazed that so many people immediately want to find the code that executes the fastest. If ONE MILLION iterations STILL take less than a second to process, is this going to be in ANY WAY noticeable to the end user? Not very likely.

Premature optimization = FAIL.

I’d go with the String.Format option, only because it makes the most sense from an architectural standpoint. I don’t care about the performance until it becomes an issue (and if it did, I’d ask myself: Do I need to concatenate a million names at once? Surely they won’t all fit on the screen…)

Read More  Code crash in MS Visual Studio 2005 in RELEASE configuration

Consider if your customer later wants to change it so that they can configure whether to display "Firstname Lastname" or "Lastname, Firstname." With the Format option, this is easy – just swap out the format string. With the concat, you’ll need extra code. Sure that doesn’t sound like a big deal in this particular example but extrapolate.

That’s the answer String output: format or concat in C#?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Read More  Hidden Features of C#?

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you