HomeLinuxPrimary consumer Enter and Output utilization in C#

Primary consumer Enter and Output utilization in C#


The 2 fundamental parts of computing that make it doable for people to work together utilizing this system’s code are consumer enter and output. In C#, console enter can be utilized to take consumer enter and console output can be utilized to show output. This text covers the basics of each phrases in C#.

What’s Consumer Enter in C#?

In C#, consumer enter may be obtained through the use of the console enter. The Console.ReadLine() operate is used for dealing with consumer enter. This strategy reads a string through the immediate window and delivers it again as a string.

Syntax

The Console.ReadLine() has the next syntax:

string var_name= Console.ReadLine();

On this case, string refers back to the information sort that represents the enter from the consumer utilizing var_name which is able to comprise the consumer’s enter worth utilizing Console.ReadLine() technique.

Instance

Right here’s an illustration of a easy program that takes a message (enter) from the consumer and saves it within the textual content variable and reveals it on the console:

utilizing System;
public class HelloWorld
{
    public static void Most important(string[] args)
    {
        Console.WriteLine(“Please Enter a message: “);
        string textual content= Console.ReadLine();
        Console.WriteLine(“The message is:” + textual content);
    }
}

The Console.WriteLine() technique has been employed within the above instance; it should present the message “Please Enter a message: ” on the terminal. The Console.ReadLine() operate is then applied to just accept consumer enter and reserve it in a string variable referred to as “textual content”. Lastly, the Console.WriteLine() operate is known as to indicate the message “The message is:” adopted by the consumer’s enter (textual content).

Word: The Console.ReadLine() operate solely accepts a single line of textual content, except the consumer presses the Enter key. If the consumer sorts multiple phrase, the entire line is saved in a string variable.

What’s Consumer Output in C#?

In C#, consumer output refers to information or info that’s offered or output to the consumer on this system’s execution. The consequence can take many various types, together with textual content, numbers, symbols, and even graphical representations. In C#, Console.WriteLine() is utilized for consumer output that’s proven on screens and is incessantly used to indicate the consumer the result of a calculation, the present state of a program, or different very important info. This technique delivers a string to the console, accompanied by a line terminator.

Syntax

Under is the straightforward syntax that shows the consumer’s message on the terminal when writing it on the C# compiler:

Console.WriteLine(“Hiya”);

Instance

The next C# program reveals the sum of two variables corresponding to num1 and num2 within the output:

utilizing System;
public class HelloWorld
{
    public static void Most important(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 within the instance above to indicate the expression “The sum of num1 and num2 is” with the sum of num1 and num2, which is the same as 50. The string is constructed with the concatenation operator (+).

Conclusion

This text covers the idea of consumer enter alongside consumer output in C#. The Console.ReadLine() technique accepts information supplied by the consumer’s keyboard and the Console.WriteLine() to current the output to the consumer. These strategies are fundamental to C# programming and are extensively utilized in interactive programming functions.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments