-2

I'm learning c# and i've seen it {0},{1} [...]. What it means?

Example:

    Console.WriteLine("\nContains(\"1734\"): {0}",
    parts.Contains(new Part {PartId=1734, PartName="" }));
  • Take a look at [C# Composite Formatting](https://learn.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting) feature. – Avestura Jul 24 '18 at 18:41

2 Answers2

0

Placeholders for argument substitution during string format. Those will be replaced with actual values. In your case x, y, z, result. You can write:

Console.WriteLine("The average of {0}, {1} and {2} is: {3}", x, y, z, result);

as

Console.WriteLine(string.Format("The average of {0}, {1} and {2} is: {3}", x, y, z, result));

Which can be optimized using latest C# syntax (feature is called string interpolation):

Console.WriteLine($"The average of {x}, {y} and {z} is: {result}");

This last line is pretty clear for what it does.

You can optionally specify output style (number of significant digits, leading zeroes etc.) - see this for syntax and examples: https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
0

If you run it you will see the result. It will put x, the parameter at 0 index in the {0} It will put y in the {1} and so on Best to play around. Although most people use the $" the value of X is {x}";

Ashley Kilgour
  • 1,110
  • 2
  • 15
  • 33