-1

My instructions were to write a C# program to print on screen the output of adding, subtracting, multiplying and dividing of two numbers which will be entered by the user.

The lines of code below seemed to be the simplest way to input values and print them out. Is {0} + {1} = {2} an expression innately built into C#? I'm not sure how num1 and num2 are being picked up as 0 and 1 in this instance and the answer as 2.

Console.Write("Enter a number: ");
int num1= Convert.ToInt32(Console.ReadLine());
Console.Write("Enter another number: ");
int num2= Convert.ToInt32(Console.ReadLine());

Console.WriteLine("{0} + {1} = {2}", num1, num2, num1+num2);
Console.WriteLine("{0} - {1} = {2}", num1, num2, num1-num2);
Console.WriteLine("{0} x {1} = {2}", num1, num2, num1*num2);
Console.WriteLine("{0} / {1} = {2}", num1, num2, num1/num2);
Console.WriteLine("{0} mod {1} = {2}", num1, num2, num1%num2);
//10 + 2 = 12                                                                                                   
//10 - 2 = 8                                                                                                    
//10 x 2 = 20                                                                                                   
//10 / 2 = 5                                                                                                    
//10 mod 2 = 0 
pensum
  • 980
  • 1
  • 11
  • 25
Rython
  • 46
  • 1
  • 7
  • 1
    Built in to the BCL https://learn.microsoft.com/en-us/dotnet/standard/base-types/formatting-types?view=netframework-4.8#composite-formatting – ta.speot.is Nov 12 '19 at 03:41
  • 2
    well, for starters, have you read the documentation? it is very comprehensive... https://learn.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting?view=netframework-4.8 – Keith Nicholas Nov 12 '19 at 03:42
  • This `"{0} + {1} = {2}` is formatted message, so `{0}` refers to first index which is `num1` in your code.. – Saif Nov 12 '19 at 03:47
  • 1
    Related question: [String.Format - how it works and how to implement custom formatstrings](https://stackoverflow.com/questions/10512349/string-format-how-it-works-and-how-to-implement-custom-formatstrings) – ProgrammingLlama Nov 12 '19 at 04:17

2 Answers2

1

The expression {0} + {1} = {2} in this line:

Console.WriteLine("{0} + {1} = {2}", num1, num2, num1+num2);

Does not mean 0 + 1 = 2. The {0} means the first argument after the comma (which is num1). In general, {x} where x is an integer greater than or equal to 0, means the x argument after the comma, where the first argument is {0}, the second one is {1} and so on. This is due to C# having zero based indexing, which basically means that the index (or position) of an item in a collection (or set) starts at 0 for the first item, 1 for the second and so on...

The string "{0} + {1} = {2}" is a formatted string, where the {x} is substituted by the x argument after the coma. Therefore, in the earlier line of code, if num1 = 7 and num2 = 4, then it would print: 7 + 4 = 11, because {0} = num1 = 7, {1} = num2 = 4 and {2} = num1 + num2 = 7 + 4 = 11.

The same goes for the other Console.WriteLine lines of your code.

Here's another example:

Console.Write("Enter your name: ");
string name= Console.ReadLine();
Console.Write("Enter your last name: ");
string lastName= Console.ReadLine();
Console.Write("Enter your age: ");
int age = Convert.Int32(Console.ReadLine());

Console.WriteLine("Your name is {0} {1} and your age is {2}", name, lastName, age);

Here, {0} = name, {1} = lastName and {2} = age.

Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21
0

Console.WriteLine is a method that takes parameters

Console.WriteLine is 'just' a method that takes parameters, which are evaluated when the method is called.

Please consider the following example.

static void PrintSum(int num1, int num2, int result) {
    Console.WriteLine("{0} + {1} = {2}", num1, num2, result);
}

PrintSum(num1, num2, num1+num2);
// 10 + 2 = 12

params object[]

In fact, WriteLine can accept params object[] and when WriteLine is called the arguments are already evaluated. I hope the snippets below helps to showcase what's happening.

Snippet 1

static void WriteLineWrapper(string template, params object[] objects) 
{
    for(int i = 0; i< objects.Length; i++ )
    {
        Console.WriteLine($"{i}: {objects[i]}");
    }
    Console.WriteLine(template, objects);
}

(...)

WriteLineWrapper("{0} + {1} = {2}", num1, num2, num1+num2);

Output

0: 10
1: 2
2: 12
10 + 2 = 12

Snippet 2

static void Print(params object[] toPrint) {
    // Note the change of order 0,2,1,3, not 0,1,2,3
    Console.WriteLine("{0} {2} {1} = {3}", toPrint[0], toPrint[1], toPrint[2], toPrint[3]);
}

static void Main(string[] args)
{
    int num1 = 10;
    int num2 = 2;
    Print(num1, num2, "+", num1+num2);
    Print(num1, num2, "-", num1-num2);
    Print(num1, num2, "*", num1*num2);
    Print(num1, num2, "/", num1/num2);
    Print(num1, num2, "mod", num1%num2);

    Print(num1, num2, "+"); // Oops
}

Output

10 + 2 = 12
10 - 2 = 8
10 * 2 = 20
10 / 2 = 5
10 mod 2 = 0
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.

String interpolation

BTW. You could use $ - string interpolation (C# reference).

Console.WriteLine($"{num1} + {num2} = {num1+num2}");
Console.WriteLine($"{num1} - {num2} = {num1-num2}");
Console.WriteLine($"{num1} x {num2} = {num1*num2}");
Console.WriteLine($"{num1} / {num2} = {num1/num2}");
Console.WriteLine($"{num1} mod {num2} = {num1%num2}");
tymtam
  • 31,798
  • 8
  • 86
  • 126