| by Arround The Web | No comments

Basic user Input and Output usage in C#

The two basic components of computing that make it possible for individuals to interact using the program’s code are user input and output. In C#, console input can be used to take user input and console output can be used to display output. This article covers the fundamentals of both terms in C#.

What is User Input in C#?

In C#, user input can be obtained by using the console input. The Console.ReadLine() function is used for handling user input. This approach reads a string via the prompt window and delivers it back as a string.

Syntax

The Console.ReadLine() has the following syntax:

string var_name= Console.ReadLine();

In this case, string refers to the data type that represents the input from the user using var_name which will contain the user’s input value using Console.ReadLine() method.

Example

Here’s an illustration of a simple program that takes a message (input) from the user and saves it in the text variable and shows it on the console:

using System;
public class HelloWorld
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Please Enter a message: ");
        string text= Console.ReadLine();
        Console.WriteLine("The message is:" + text);
    }
}

The Console.WriteLine() method has been employed in the above example; it will show the message “Please Enter a message: ” on the terminal. The Console.ReadLine() function is then implemented to accept user input and save it in a string variable called “text”. Lastly, the Console.WriteLine() function is called to show the message “The message is:” followed by the user’s input (text).

Note: The Console.ReadLine() function only accepts a single line of text, unless the user presses the Enter key. If the user types more than one word, the complete line is saved in a string variable.

What is User Output in C#?

In C#, user output refers to data or information that is presented or output to the user on the program’s execution. The result can take many different forms, including text, numbers, symbols, and even graphical representations. In C#, Console.WriteLine() is utilized for user output that is shown on screens and is frequently used to show the user the outcome of a calculation, the current state of a program, or other vital information. This method delivers a string to the console, accompanied by a line terminator.

Syntax

Below is the simple syntax that displays the user’s message on the terminal when writing it on the C# compiler:

Console.WriteLine("Hello");

Example

The following C# program shows the sum of two variables such as num1 and num2 in the output:

using System;
public class HelloWorld
{
    public static void Main(string[] args)
    {
        int num1 = 30;
        int num2 = 20;
        int sum = num1 + num2;
   Console.WriteLine("The sum of  num1 and num2 is " + sum);
    }
}

The Console.WriteLine() is used in the example above to show the expression “The sum of num1 and num2 is” with the sum of num1 and num2, which is equal to 50. The string is built with the concatenation operator (+).

Conclusion

This article covers the concept of user input alongside user output in C#. The Console.ReadLine() method accepts data provided by the user’s keyboard and the Console.WriteLine() to present the output to the user. These methods are basic to C# programming and are widely used in interactive programming applications.

Share Button

Source: linuxhint.com

Leave a Reply