C# Console WriteLine() and Write()Tutorial

So far we’ve used the WriteLine() method in a couple of examples in the previous sections.

This method and the class Console will be used throughout this tutorial so we thought it’s a good idea to explain this method a little bit deeper on what it does and how it does etc.

Note: Because we didn’t explain the concept of class and static methods yet, it’s hard to get deep in this section. So our explanation will be general just to give you a overall view. But in the future section this method and the Console class will be explained again in greater details.

In order to send a message to the output (which typically is the console) we use the WriteLine() method.

This method belongs to a class named Console and the method is static. Don’t worry about the word static for now. For now all you need to know is that a static method is accessible via the name of its class. For example here the owner class of the WriteLine method is the Console and so to access this method we first call the class name + a dot . operator and followed by that is the name of the static method.

Hence:

Console.WriteLine();

Note: usually between the name of a class and its static method we put only the dot . operator and there’s no space in between. But no error will occur if you add white space as well!

Now to send a value to the output stream (console for example), that value should be put in the parentheses of the WriteLine() method. For example let’s say we want to send the string value “Hello World!” to the output stream:

Console.WriteLine("Hello World!");

Inside the WriteLine() method there are instructions that the C# engine follows and eventually the value that we set as the argument to the method will appear on the output stream (console for example).

If the value that we want to send to the output stream belongs to a variable, all we need to do is to put that variable in the parentheses of this method. After that, a copy of that value will be sent to the output.

Example:

using System;

namespace quickExample

{
public class Program
{
public static void Main(string[] args)
{
string message = "Hello World!";
Console . WriteLine(message);
}
}
}

Output: Hello World!

If the number of values that we want to send to the output is more than one, we can use the addition operator + as well.

For example:

using System;

namespace quickExample

{
public class Program
{
public static void Main(string[] args)
{
string fullName = "John Doe";
int age = 1000;
Console . WriteLine(fullName+ " "+age);
}
}
}

Output:

John Doe 1000

Here 3 values are added together and sent to the console.

The first value belongs to the fullName variable, the second value is a white space and the third value belongs to the age variable.

As you can see, between all the three values, we’ve added the addition + operator.

When the compiler sees this operator, it knows that it should put together the involved operands (values in the operation) and create one final result. Then this result will be the value of the WriteLine() method and hence appears on the console.

For example if we have two integer values as the operands of the addition + operator, first they will be added together and then the final value will be sent to the output:

using System;

namespace quickExample

{
public class Program
{
public static void Main(string[] args)
{
int val1 = 100;
int val2 = 200;
Console . WriteLine(val1+val2);
}
}
}

Output: 300

If for whatever reason we don’t want these values to be added together and just appear on the console separately, we can put a white space String value in between (add the integer values to this whitespace) and in that case the C# engine won’t apply the addition operation on the two integer values.

Example:

using System;

namespace quickExample

{
public class Program
{
public static void Main(string[] args)
{
int val1 = 100;
int val2 = 200;
Console . WriteLine(val1+" "+val2);
}
}
}

Output:

100 200

Note: you’ll learn more about the addition operator in the Arithmetic operators.