C# Output | CodeGuru.com

C# Output

In the previous part of our ongoing C# programming tutorial series, we learned how a developer can perform input operations leveraging the Console class. In this article, we will demonstrate how a programmer can print output in a C# application. Let’s first look at the different methods and classes used for creating output in the […]

Written By
Tariq Siddiqui
Tariq Siddiqui
Oct 7, 2022
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

C# Programming Guide

In the previous part of our ongoing C# programming tutorial series, we learned how a developer can perform input operations leveraging the Console class. In this article, we will demonstrate how a programmer can print output in a C# application. Let’s first look at the different methods and classes used for creating output in the popular programming language.

You can read or review the previous part of this C# tutorial by visiting: C# User Input.

How to Print User Output in C#

In the System namespace, there is a Console class, which contains several built-in methods. These methods allow programmers to get user input and produce output to the console window. Two of the most common methods used for the purpose of standard input/output (I/O) operations are:

  • Write()
  • WriteLine()

To better understand the practical use of both of these methods, observe the following code example, which shows how to print output to the console window in C#:

using System;
namespace IOProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hi there!");
        }
    }
}

If you run this code, you get the following output:

Hi there!

The code uses the WriteLine() method to print the output – in this case the text written between the two quotation marks – to the standard output stream or console.

What is the Difference Between Write() and WriteLine() in C#?

The Write() and WriteLine() methods serve the same purpose – producing output to the console. The only difference between the two is that the Write() method only prints the provided string to the console window, while the WriteLine() method not only prints the provided string, but also moves the prompt to the start of the next line in the console.

Let’s understand the difference by examining the output of the following example code:

using System;
namespace IOProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Printing using WriteLine ");
            Console.WriteLine("Places this text in a new line");
            Console.Write("Printing using Write ");
            Console.Write("… places it in the same line");
        }
    }
}

Running the above code snippet in your integrated development environment (IDE) or code editor would result in the following output:

Printing using WriteLine
Places this text in a new line
Printing using Write … places it in the same line

Read: Best Online Courses to Learn C#

Advertisement

How to Concatenate Strings in C# using Write() and WriteLine()

You can concatenate – or join – two or more strings in C# by using the + operator inside the WriteLine() or Write() methods. Here is an example showing string concatenation in C#:

using System;
namespace IOProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            double digit = 77.32;
            Console.WriteLine("This is how strings are combined " + "in C#");
            Console.WriteLine("Given digit is = " + digit);
        }
    }
}

Running the above code results in the following output:

This is how strings are combined in C#
Given digit is = 77.32

The above code demonstrates how programmers can combine two or more strings using the + operator. You can concatenate or combine strings in two ways. The first one is using the + operator which we have seen, or via a second method that is more efficient, which we will discuss in the following section.

Other Ways to Concatenate Strings in C#

There is an alternate way to concatenate strings in C# and that is using a formatted string. Notice in the example below, we will be using placeholders for variables. When the code executes, the placeholders get replaced by the corresponding variables. To make it clear, consider the following code snippet:

class Program
    {
        static void Main(string[] args)
        {
            int x = 7, y = 5, mul;
            mul = x*y;
            Console.WriteLine("{0} x {1} = {2}", x, y, mul);
        }
    }

When you run the program, {0} will be replaced by x, {1} will be replaced by y, and {2} will be replaced by the multiplication variable mul. As you can see, the approach we used in the above code is more readable and less prone to errors when compared to using the + operator to concatenate strings.

Advertisement

Printing Variables and Literals in C#

The Write() and WriteLine() methods can be used for printing literals and variables as well. The following program demonstrates how you can perform this standard output operation in C#:

    class Program
    {
        static void Main(string[] args)
        {
            int x = 55;
            Console.WriteLine(x);	 // prints variable
            Console.WriteLine(0.006);  // prints literal value
        }
    }

Running the above code results in the following output:

55
0.006

In this code, we first created an integer named x and assigned it the value 55. We then used WriteLine() to print out the value of our variable.

In our next statement, we passed a literal value – 0.006 – to the WriteLine() function, which was subsequently printed to the user’s screen.

Read more C# programming tutorials and software development tips.

Tariq Siddiqui

A graduate in MS Computer Applications and a Web Developer from India with diverse skills across multiple Web development technologies. Enjoys writing about any tech topic, including programming, algorithms and cloud computing. Traveling and playing video games are the hobbies that interest me most.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.