c sharp

C# Console.WriteLine

To display data on the console terminal from the source code in C sharp programming language, we need a specific built-in feature that is used only as a piece of statement. All the contents that we want to display are shown on the console by using this. WriteLine() function displays data written inside the parentheses as arguments on the console with the help of the console class. This article will contain some elementary examples to explain the working of this function.

Example # 1

To elaborate on the working of the console.writeline, we have used a simple example here in which we will display three words on separate lines. A ‘system’ class is used first in the program to enable the use of classes and other data structures.

Writeline is a built-in function in C sharp programming language that is used to print values on the console. Inside the main program, this function will play the role of displaying the values. The function WriteLine() is present in the Console class to display the resultant value on the console, so we use the Console keyword with the function to call the function. The word that you want to display is written inside the parameter of the function as an argument.

Console.Writeline("Aqsa");

Similarly, two other words will be displayed likewise using this function. For the execution purpose, we use a compiler to compile the c sharp code; MCS is used for this. After the compilation process, we execute the file with the .exe extension. This is done through mono.

$ MCS file.cs

$ Mono file.exe

On execution, you will observe that we have displayed each word on a separate line.

Just like WriteLine() function, there is a simple write() function in C sharp that is used to display the content on a single line. So if we remove the ‘Line’ word from the writeLine() function, it will behave differently. This function is also accessed through the console class. For example, we will use the above three words for the write() function to see the difference between these two functions.

Console.Write("Aqsa");

Now we will execute the above source code. You will see that although we have used the function call in three separate lines. It does not affect the result because the write() function works in such a way that it displays all the content in a single line, no matter how many spaces or tabs you used for separate lines. Each content will be a part of the same line.

Example # 2

The writeline() function doesn’t need to display values every time it is used. This is also used to display a blank line. In most cases, while displaying data on the console, you need to create gaps between two irrelevant data or create space for the next items to get displayed. So we use the writeline() function for this purpose as well. We are using this phenomenon as an example.

In addition to the ‘system’ library, we have used two more basic header files of C sharp. These two libraries play an essential role in displaying results on the console of Linux. ‘system.IO’ is used for the input and output streams; when we need to take input from the user, we prefer to use this library. ‘system. Text’ is used in the case of text input.

Using System.IO;

Using System.Text;

After defining the namespace, we are heading towards the main program; a simple line is displayed through the console.writeline() function. After that line, we need to display a blank line, so we simply use the writeline function with no arguments in the parameters.

Cosole.WriteLine();

This function will work in such a way that it will print only the next blank line. Then, to clarify, we will display another text line to show the gap between these two lines created by using a simple function call of the writeline() function with no arguments.

Another feature, just like the writeline() function we have used here. As the writeline function is for the output, we use a built-in function of C sharp is Console to have any input from the user at the console.ReadLine(). It takes the value entered at the console as input from the user. The next example will explain this feature of taking input and using that input later. In this example, we have simply used this function to take any input key so that the program may stop execution. Here user will enter through the tab, and then the function will stop running.

Example # 3

This function will use the involvement of the user; all three same libraries are used here, as we have explained in the previous example. In the main program, a string-type variable is declared. This variable will store the value that the user will enter at the time of execution of the program. Through console.writeline() function, we will display a simple text message to the user so that the user can enter a value to console to be stored in the variable. Then through the Console.ReadLine() function, the user input will be taken from the console and will be saved in the variable we have declared above; this will be done through an assignment operator.

Input = Console.ReadLine();

This stored line will be displayed through the writeline function on the console. In the previous examples, you have seen that a string is directly displayed through the writeline function. But we can also display the value stored inside the variable.

So on execution, you will see that the variable will display the value present inside it.

Example # 4

By using the writeline function, we can also use the value itself, known as the literal, and the value through the variable. This is done through the concatenation process. It is the approach of joining two strings with one another. This concatenation function is the built-in feature of strings mainly that is used in C sharp. It is not necessary that both the values used for joining purposes must be of strings data type. The value displayed directly is a string, and the other value is a variable name, that is, an integer; both are joined through a concatenation process.

In this example, inside the main program, we will use a variable that has stored the age. An integer value is assigned to it. This will be used later in the example. First, we have used both the literals of our choice to be combined. In the second statement, we have used a variable name and a literal directly.

Conclusion

Console.WriteLine() is a function that is used in C sharp to display the contents of source code on the console screen with the help of a console class. In this article, we have applied several different approaches to explain the working of this function in detail. Writeline() function can display data directly on the screen in the form of any string statement. On the other hand, we can also display a blank line with the help of this function. The value entered by the user is also entertained through this function. In this way, we have seen that console. The writeline function is used in several ways to display content on the console.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.