C# Print: Get a Grip Printing an Output Line
C# was designed to enable developers to build powerful applications that run on the Microsoft .NET framework. The syntax of the language is similar to that of C and C++. All the advantages of Java, C and C++ – languages which were designed earlier- are present in C#. This language can be used to create Windows based applications, client-server apps, Web services that rely on XML, database apps and distributables, among other things. C# programs need the .NET framework to run – all C# code is compiled and converted into an intermediate language (IL) that can be understood by the common language runtime (CLR) used by .NET. You can see how C# and .NET work together in this course.
C# is really simple to learn if you have some experience with C, C++ and/or Java. The language relies heavily on these three languages for inspiration. If you’ve studied even one of these three languages before, you will have little trouble picking up the basics of C#. Like Java, C# is an object oriented language that supports inheritance, polymorphism and encapsulation. A class in C sharp can inherit from one class only, but several interfaces may be derived from it. C# code is much less complex than C++ code, and you can do more with it as well. There are many resources available online that can help you learn the language. We recommend that you take this C# course if you’re new to programming and don’t want to waste your time scouring the internet for bits and pieces. You will learn everything you need to know to become a good C# programmer and you will find all the information in a single place.
In this basic tutorial, we’re going to see how we can use C# to print an output line(s). You’ll find it easier to follow this tutorial if you’re familiar with the basics of C#. If not, you may want to first hop over to this C# tutorial for beginners.
Using C# to Print Output
The syntax we use to print something to the console in C# is:
System.Console.WriteLine(“Your Message Here”);
Just like in C, the print line statement is followed by a semicolon (“;”). The WriteLine method present in the System.Console class will print our message to the console. The WriteLine method is a static method that is present in the console type.
Example 1: First, let’s write a simple program that displays the message: “Welcome to C sharp”.
//print1.cs
public class print1
{
public static void Main()
{
System.Console.WriteLine (“Welcome to C sharp);
}
}
Output: Welcome to C sharp
This program is easy enough to understand. In C#, the main method must be contained within a class. In our example, we have used a class called print1 and placed the main method inside it. Finally, we’ve printed a simple message to the user with the System.Console.WriteLine statement.
Example 2: What if you had multiple statements to print and display to the user? It would be tedious to type “System.Console.WriteLine” every single time you wanted to print something to the screen. In this case, you can just qualify the System.Console class once before the start of the program. After that, every time you need to display a message, the Console.WriteLine statement will suffice. Take a look at the example to understand the concept better:
// print2.cs
using System;
public class print2
{
public static void Main()
{
Console.WriteLine("Welcome to C Sharp");
Console.WriteLine(“We Hope You Enjoy the Experience”);
}
}
Output: Welcome to C Sharp We Hope You Enjoy the Experience
Do you see what we did? We qualified the class System at the beginning of the program using the “using” keyword. Now every time we use the “System.Console.WriteLine” statement, we can omit the system keyword.
Example 3: Now let’s write a simple program to print out integer and Boolean values.
// print3.cs
using System;
public class print3
{
public static void Main()
{
int integerval = 4;
Console.WriteLine(integerval);
bool boolval = false;
Console.WriteLine(boolval);
}
}
Output: 4
false
Like in the previous program, we qualified the system class at the beginning of the program. Next, we created an integer variable and a Boolean variable. And finally, we printed them using the Console.WriteLine method.
Example 4: You can use Console.WriteLine to print an empty (blank) line if you don’t specify any arguments for it.
// print4.cs
using System;
public class print4
{
public static void Main()
{
int integerval = 4;
Console.WriteLine(integerval);
Console.WriteLine();
bool boolval = false;
Console.WriteLine(boolval);
}
}
Output: 4
false
This program is identical to the previous program, except for a single Console.WriteLine() method we inserted between the other two Console.WriteLine() methods. Because we didn’t specify any arguments for this method, there was a blank space – a gap- between the two messages in the output.
Example 5: C sharp is designed to let you make attractive applications. There are many ways you can brighten up your program. One method is by changing the background color and the foreground color. Changing these colors will make your printed message stand out. Using colors in your program will also make it much easier to spot errors.
// print5.cs
using System;
public class print5
{
public static void Main()
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
int integerval = 4;
Console.WriteLine(integerval);
bool boolval = false;
Console.WriteLine(boolval);
Console.ResetColor();
}
}
Output: 4
false
We added two new methods to this program: Console.BackgroundColor and Console.ForegroundColor. We assigned the color red to the background and the color white to the foreground. Finally, after the message has been output, the console colors are reverted to default with the line: Console.ResetColor. To get a better idea of how your output looks, we recommend you type it out for yourself. If you’re unfamiliar with how to run a C# program, you can sign up for the first part of this comprehensive C# course.
Example 6: You can use the Console.WriteLine method to print an array on the screen.
// print6.cs
using System;
public class print6
{
public static void Main()
{
char[] chararray = new char[] {‘x’, ‘y’, ‘z’);
Console.WriteLine (chararray)
Console.WriteLine(chararray, 0, 2)
}
}
Outuput: x, y, z
x, z
Printing an array is an advanced (and very useful) function of the WriteLine method. In the program, we’re creating a character array called chararray that has three arguments: x, y and z. You can specify as many character arguments as you want, of course, but we’ve just used three to make the program more understandable. The first Console.WriteLine statement will print out the entire character array: x, y and z. The second Console.WriteLine statement will, however, print out the first (0) and the third (2) characters of our array: x and z. If you’re unfamiliar with arrays, they are always counted from 0 upwards. In our array, the letter x will be considered to have the first (0) position, while the letter z will have the last (2) position. The letter y, which we haven’t printed, will have the second (1) position.
You will be using the System.Console.WriteLine method in almost every program you write in C#, so it’s a good idea to get a good grip on the different ways in which it can be used. Of course, you’ll also need to understand the other methods available in C#, and how to use them. We strongly recommend you follow up with PartII of our Learn C# series to get a better understanding of the language. Once you’re ready to move on to the advanced level, take Part III of our Learn C# series to master C#.
Recommended Articles
Top courses in C# (programming language)
C# (programming language) students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.