# Console.Write()
It is a simple method that you can use to print any value.
Run this program in your IDE. What do you see?
# Exercise 1
using System;
class MainClass
{
public static void Main ()
{
Console.Write(0); // 0
Console.Write(-2); // -2
Console.Write(10 / 5); // 2
Console.Write("Hello"); // Hello
Console.Write(1 != 2); // True
Console.Write(4 == 2 * 2); // True
Console.Write(14 == 7); // False
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Newline character
Console.Write
does not write a newline character. As a result, output of the above program is on a single line. You can use \n
to add newline.
Console.Write("\n");
Console.Write("Hello World!\n");
1
2
2
# Exercise 2
What is the output of this program? Answer it without running it in your IDE.
using System;
class MainClass
{
public static void Main ()
{
Console.Write("My name is ", "Talha");
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Now run the program in the IDE.
- What is the output?
- Does it match your expectation?
If the output does not match your expectation, then don't worry. We will discuss it in further detail Composite Formatting.