0

In this code:

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

How is this {0} called?

I'm starting in C# and I've seen some codes with this.

I kind of know how it works, but why should I use ("Hello {0}", name) instead of ("Hello " + name)?

Emanah
  • 56
  • 4
  • [Console.WriteLine](https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=netcore-3.1) [Composite formatting](https://learn.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting) – bolov Sep 11 '20 at 02:18
  • 1
    You can also do `$"Hello {name}"`. That said, `string.Concat` is faster than `string.Format`, which is what you are comparing. – Zer0 Sep 11 '20 at 02:18
  • 1
    You can also format strings directly - `Console.WriteLine("Hello {0:0.00}", 3.14159)` will write out `Hello 3.14`. – Enigmativity Sep 11 '20 at 02:19
  • 1
    `{0}` is called [Composite Formatting][1]: whereas the `$` method is called [String Interpoation][2] Take a look at the documentation if you want to see all the variety of ways you can use these options. IMO, you will end up using composite formatting quite a bit. [1]: https://learn.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting [2]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated – Pete -S- Sep 11 '20 at 02:25
  • Ok, guys, got it! Thanks for the answers, they were all really helpful! – Emanah Sep 11 '20 at 02:27
  • Also, if you have many parameters to be printed the code becomes very hard to read if formatted like "hello " + name + " " + address + andSoOn... The string interpolation provided in answers is the most readable way. – Saplu Sep 11 '20 at 02:44

2 Answers2

4

So, the following is the Format args pattern which is similar to String.Format

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

// or

Console.WriteLine(string.Format("Hello {0}", name));

if you look at the source code to this overload of WriteLine, you will find actually it calls String.Format

WriteLine(string.Format(FormatProvider, format, arg0));

You could however use

Console.WriteLine("Hello " + name)

There is no real difference 1, however you miss out on all the extra format abilities of String.Format (though in your case you are not using any of them anyway)

You could also use the more modern string interpolation

Console.WriteLine($"Hello {name}")

As to what you want to use, depends on what you need to do, and what you are comfortable with. All have their advantages and disadvantages


Further reading

$ - string interpolation (C# reference)

String.Format Method


1 As noted by @Zero String.Format actually does a lot internally, so it's slower.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • 1
    "There is no real difference". Functionally, no. Performance wise, yes. `string.Format` is measurably slower. Whether such a [relatively] small hit matters depends on the code. – Zer0 Sep 11 '20 at 02:27
  • @Zer0 updated and attributed, thanks – TheGeneral Sep 11 '20 at 03:01
1

It is called string interpolation. Below is a useful reference

https://dotnetcoretutorials.com/2020/02/06/performance-of-string-concatenation-in-c/

Jimmy
  • 46
  • 3