Methods and Properties of Console Class in C#
What is Console Class in C#?
Properties of Console Class in C#.
Methods of Console class in C#.
Understanding the use of Write and WriteLine method in C#.
Program to show how to print the value of a variable in a console application.
Understanding the use of the ReadLine method in C#.
Program to show the use of BackgroundColor, ForegroundColor and Title properties of Console class.
What is Console Class in C#?
In order to implement the user interface in console applications, Microsoft provided us with a class called Console. The Console class is available in the “System” namespace. This Console class provides some methods and properties using which we can implement the user interface in a console application.
All the properties and methods available in the console class are static. So we can access these members by using the Console class name i.e. we don’t require Console class instance.
Properties of Console Class in C#:
Property
Description
Title
Specifies the title of the console application
Background color
Specifies the background color of the text
Foreground color
Specifies the foreground color of the text
Cursor size
Specifies the height of the cursor in the console window “1 to 100”
Methods of Console class in C#:
Method
Description
Clear()
To clear the screen
Beep()
Play a beep sound using PC speaker at runtime
Resetcolor()
Reset the background and foreground color to its default state
Write(“string”)
Display the specified message on the console window
WriteLine(“string”)
Same as write method but automatically moves the cursor to the next line after printing the message.
Write(variable)
Displays the value of the given variable
WriteLine(variable)
Displays the value of the given variable along with moving the cursor to the next line after printing the value of the variable.
Read()
Read a single character from the keyboard and returns its ASCII value. The Datatype should be int as it returns the ASCII value.
ReadLine()
ReadKey()
Reads a string value from the keyboard and returns the entered value only. As it returns the entered string value so the DataType is going to be a string.
This method reads a single character from the keyboard and returns that character. The Datatype should be int as it returns the ASCII value. It is a STRUCT Data type which is ConsoleKeyInfo.
Example: Program to show how to print the value of a variable in a console application.
Example: Program to show how to read the value at runtime in a console application.
Example: Program to take two numbers as input from the console and then print the summation in the console.
The ReadLine method always accepts the value in the form of a string. So we need to convert the values to the appropriate type. In the above example, we are converting the values to integer type by using int.Parse and Convert.ToInt methods.
Example: Program to accept employee details like empno, name, salary, address, job and print the accepted information.
Example:
Program to accept student no, student name, mark1, mark2, mark3 and calculate the total mark and average marks and printing accepted information.
Example:
Program to show the use of BackgroundColor, ForegroundColor and Title properties of Console class in C#.
Last updated
Was this helpful?