To print the value to the console, we used source codes on the Linux platform, so we needed the Ubuntu Console terminal to display the resultant values. The user must access the text editor and the terminal to understand the phenomenon. This tutorial will provide different aspects of displaying content on the Ubuntu Console.
Example 1
To implement the concept of printing any value or message to the Linux operating system console, we have chosen the simplest example to elaborate on. In C sharp, built-in functions are used for several operations to be carried out. Similarly, there are two ways to display anything to the console that will appear on the user’s screen.
A write function is used to display content on the console. This method is written as a console statement. The parameters of this function contain any string statement or any variable from which you want to display the values.
By using this, we will be able to see the string on the console when the code will be executed. Similarly, we have used three strings to be displayed by using his write function.
Save this code once with the .cs extension. Then go to the terminal and then use the MCS compiler to compile the c sharp source code; after the compilation, we will use a mono executer for the execution.
You can see that on execution, all the three strings are displayed without any space making a single string that lies in the same line. Although we have used each console.write a statement on a separate line so that expected results would have each line in a separate line; this does not happen. The write() function only displays the content in a single line without going to the next line. To display data in a separate line, we use another feature of C #, which is writeLine().
C # WritLine()
Like the write() function, writeline is another function used to display the contents in the source code towards the terminal through a console statement. This function also takes the value to be displayed in the parameter.
We have used three strings to get printed on the console using this function.
After the code is completed, save it, and then we will execute this source code on the terminal.
On the execution, you can see that all the strings are displayed on a separate line instead of written on the same line without being separated by a space. The writeLine() function displays the content or string along a line. Every time this function is used, the contents in the parameters will be displayed on the next line following the first string.
Example 2
There are several ways to print data to the console. We can also bring the literals to present inside the variable and the string or any integer value directly used as a parameter of the writeline function. For this purpose, write() and writeLine() functions are used. These two are the basic terminologies required to approach the console through a source code.
We will use the name of this variable inside the parameter. The value assigned to it will be displayed on the console through this variable.
# Console.witeline(value);
On the other hand, the literal can be displayed directly; if we provide any value without assigning it to the variable, it will be displayed directly.
On the execution of the code, we will see that the resultant value is displayed on a separate line because of the writeline(). And the value of the variable is also accessed and displayed.
Example 3
Concatenation is a phenomenon in strings, where we have a process of combining or adding two strings with one another by using a ‘+’ operator. In this example, the concatenation is followed through displaying the literals and variables phenomenon, as we use an integer type variable to store the age. The person’s name is used directly as a literal in the function parameter.
The concatenation will be applied to the literal. We have used the first and the second names separately as two literals. These will be combined through the plus operator.
In the second string, the concatenation combines the string ‘value’ with the variable’s value, as the variable is not the string, so it is not quoted through inverted commas.
Using this, the value of the variable ‘age’ will be fetched and displayed in the place of the age.
Now execute the code; you will see from the results that two separate strings are combined through concatenation. The value from the variable and the string are also combined through concatenation. So it is proved that a concatenation process is not for the strings to get combined directly, but the literal and the variables can be used for the same purpose.
Example 4
Not only do we assign values to the variables directly in the source code, as used in the previous examples, but also we can take values from the user. User involvement plays an important role in taking data from the console and then printing it to the console. Here the phenomenon of using the variable is the same. The user will enter input stored in the variable declared initially in the program. Write (), and writeLine() are two functions to print to the console. Similarly, ReadLine() is a built-in feature in C # to get value from the console. So this function will be used to get the value from the user.
Let us start with the source code description.
Inside the main program, declare a string variable. Through the console class, display the statement to the console so that the user can enter a value after seeing this string.
The user will enter the value, which will be stored in the string type variable.
After the value is stored in the variable, we will display it using the specified variable name in the console string with the WriteLine() function.
As we have used a simple write() function, you can see that the user input is displayed on the same line as the string displayed first.
Conclusion
Printing data to a console is an important field of implementing programs of C sharp in the Ubuntu operating system. Because the console is the platform for directly getting outputs of the code. Several ways are incorporated to display the values towards the console. Two basic features for displaying data are discussed here an input ReadLine() function. Because besides these input and output features, it becomes hard to display and get the value to/from the console. Similarly, concatenation and displaying literals and variables are also associated with print to console.