1

I am relatively new to programming but have noticed a lot of people when creating strings using variables do something like the below to put a variable into a string. I am curious at the difference/reason why this seems to be preferred by the senior developers I know and to also get opinions on why to use one over the other.

var x = "world";
Console.WriteLine("Hello {0}",x);

Is there an advantage or specific reason as to why the above is used as opposed to this:

var x = "world";
Console.WriteLine("Hello " + x);

Or this

var x = "world";
Console.WriteLine($"Hello {x}");
4
  • 2
    There's much more difference between Console.WriteLine(GreetingPlaceholder, x); and Console.WriteLine(GreetingPrefix + x); if you want to support multiple languages
    – Caleth
    Jan 15, 2020 at 12:38
  • There's another aspect to this: for Right-to-Left languages, or other Grammars, you might want the equivalent of "World, Hello!". The + method does not allow this - it always appends on the right-hand-side - but other methods described below do allow this. Consider the example of logging in to a personalised website and putting a personal greeting on the top of the screen. Jan 15, 2020 at 14:01
  • One thing to remember is that the variable-interpolated strings have only been a c# feature since 2015? so any code predating that will use the first two approaches. Feb 17, 2020 at 20:37
  • In my opinion ,it should be depend on your code like assume if you have small string to show you can use the second option but if there is more dynamic values to show in your string you can prefer rest of options
    – Ishan Shah
    May 19, 2020 at 7:07

2 Answers 2

4

Rather than ask, test it:

using System;

public static class Program 
{
    public static void Main() 
    {
        X();
        Y();
        Z();
    }

    public static void X()
    {
        var x = "world";
        Console.WriteLine("Hello {0}", x);
    }

    public static void Y()
    {
        var x = "world";
        Console.WriteLine("Hello " + x);
    }

    public static void Z()
    {
        var x = "world";
        Console.WriteLine($"Hello {x}");    
    }
}

Look at the IL produced from the different approaches to see what difference it makes.

The "Hello " + x approach invokes string.Concat then calls WriteLine. You could then go off and look at the source of WriteLine and you'd find it does a string.Concat anyway.

So in short, there's no real difference and you shouldn't expect there to be: the compiler will tend to sort such things out for you anyway and thus worrying about it is just premature micro-optimising on your part, which is behaviour that should be avoided.

And neither is the ideal way to do it anyway. The third approach in my code above,

var x = "world";
Console.WriteLine($"Hello {x}");    

is by far the neatest and clearest way to express it.

3
  • Console.WriteLine calls Out.WriteLine (source) which is a StreamWriter (source) which delegate WriteLine to its base (source)...
    – Theraot
    Jan 15, 2020 at 10:55
  • StreamWriter derives from TextWriter (source), which calls String.Format (source), which uses FormatHelper (source)...
    – Theraot
    Jan 15, 2020 at 10:55
  • FormatHelper uses ValueStringBuilder.AppendFormatHelper resulting in no call to string.Concat (source and source)
    – Theraot
    Jan 15, 2020 at 10:55
5

It's simply that it's easier to read (and write) for longer/more complicated strings or strings where the variable is somewhere in the middle of the string.

Console.WriteLine("Hello, {0}. Happy {1}. The time is {2}.", name, dayOfWeek, time);

vs.

Console.WriteLine("Hello, " + name + ". Happy " + dayOfWeek + ". The time is " + time + ".");

For trivial examples where a variable is just appended to a string it makes very little difference.

1
  • 4
    Of course, as David Arno pointed out in his answer, the best of both worlds can be found in Console.WriteLine($"Hello, {name}. Happy {dayOfWeek}. The time is {time}.")
    – Eric King
    Jan 15, 2020 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.