Printing program output / data on Console in C#

Console output in C# – Learn how to print program outputs in C# on console screen using Console.WriteLine method. You will learn how to print Strings, int and double variables etc on console.

Here is a simple C# program that prints output as “I love C# Programming”.

public class Sample {
 
	public static void Main(String[] args) {
		
		Console.WriteLine("I Love C# Programming");
 
	}
}

Output:
I Love C# Programming

Notice that string “I Love C# programming” is in double quote.

Printing multiple Strings on Console in C#

Here is another example of printing multiple strings in C# program.

public class Sample {
 
	public static void Main(String[] args) {
		
		Console.WriteLine("My name is Peter");
		Console.WriteLine("I am learning C#");
		Console.WriteLine("I like Coffee");
 
	}
 
}
 

Output:
My name is Peter
I am learning C#
I like Coffee

Printing variables in C# on Console

You can print value of int, double and String variables in C# program on console screen.

C# code:

 
public class Sample {
 
	public static void Main(String[] args) {
 
		
		String str = "I Like Coffee";// String variable
		int count = 2; //int 
		double cost = 99.00;//double 
 
		//Print all data on console
		Console.WriteLine(str);
		Console.WriteLine(count);
		Console.WriteLine(cost);
 
	}
 
}

Output:
I Like Coffee
2
99.0

Printing multiple values in one line on Console

You can print multiple types of data by appending them using + symbol in Console.WriteLine method in one line.

C# code example:

In below program an int variable and a string variable are appended in Console.WriteLine method using + symbol.

public class Sample {
 
	public static void Main(String[] args) {
 
		
		String str = " Coffee!!!";
		int count = 100;	
		
		Console.WriteLine(count+str);	
 
	}
}
 

Output:
100 Coffee!!!

WARNING:

Beginners make mistake of using common symbol (,) instead of (+ )symbol . For example, Console.WriteLine(count,str);

One more example below on console output in C#

that is appending a hard code string “value = “ and an int value of variable num.

public class Sample {
 
	public static void Main(String[] args) {		
		
		int num = 100;	
		
		Console.WriteLine("value = "+ num);	
 
	}
}

Output:
value= 100

public class Sample {
 
	public static void Main(String[] args) {		
		
		int num = 100;	
		
		Console.WriteLine("value = {0} ", num);	// This is same as '+' symbol(concatination)
 
	}
}
 

Output:
value= 100

NOTE: Having space before or after + symbol, does not affect the output. For example, in above program, both statements given below produces same output.

Console.WriteLine(“value = “+ num)
Console.WriteLine(“value = ” + num);

Written By Shilpa Jaroli & Reviewed by Rakesh Singh

Related Posts