- Back to Home »
- C#.NET »
- C#.NET Console Class
Posted by :
Sudhir Chekuri
Wednesday, 30 December 2015
Console
is a class under System namespace.
Under
Console class we have methods and properties which are used to create Console
application UI(User interface).
Console
Methods
These Console methods will play a prominent role in developing console applications.If System namespace is not added at the top of the program, we have to use System.Console everywhere where Console class is used.
Example
Console.Write("System is added");
System.Console.Write("System is not added");
ReadKey
ReadKey method is under Console class used to stop the output screen of a console application from closing immediately after program execution till we press a key.So use Console.ReadKey( ) line at the end of Main function in every program to see the output.
Example program to print a simple message using ReadKey
Program
using System;
namespace readkey
{
class Program
{
static void Main(string[] args)
{
Console.ReadKey();
}
}
}
Output
_
Explanation
Blank blank ouput screen waits without closing till you press a key. This is because of ReadKey method.
If that line is removed the output screen will close immediately after completion of program execution.
Write
Write method is under Console class.
It is used to print a message on the output screen of console application.
When you print a message using Write the cursor will appear in the same line next to the message.
It prints the text as it is present in the double quotes.
It also prints the value present in variable.
Example program to print a simple message using Write
Program
using System;
namespace write
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hai this is Write
example");
Console.ReadKey();
}
}
}
Output
Hai this is Write example_
Explanation
In the above program Write is a method under Console class used to print a message on the output screen with a cursor blinking next to message.
As Console class is under System namespace we have to add it at the top.
ReadKey method is used to make output screen without closing immediately.
We can also write the above program without adding System namespace as below.
Example program to print a simple message on using Write without adding System namespace
Program
namespace write
{
class Program
{
static void Main(string[] args)
{
System.Console.Write("Hai this is Write
example");
System.Console.ReadKey();
}
}
}
Output
Hai this is Write example_
Explanation
As Console class is under System namespace we have to add it at the top otherwise we have to add it before Console class everywhere in the program where we use Console class.
WriteLine
WriteLine method is under Console class used to print message on the output screen of console application and the cursor will appear in the next line.Example program to print a simple message using WriteLine
Program
using System;
namespace writeline
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hai this is WriteLine
example");
Console.ReadKey();
}
}
}
OutputHai this is WriteLine example
_
Explanation
In the above program WriteLine is a method under Console class used to print a message on the output screen with a cursor blinking in the next line of message.
Read
Read method is under Console class used to Read data of only one character from the user given in runtime.
When you read a message using Read the cursor will appear in the same line.
Return type of Read method is int ie., it returns back the data given by the user in the form of int.
It returns ascii value of the given data.
It doesn't take any parameter between the braces.
Example
int i=Console.Read();
Console.Write(i);
If the user enters a its ascii value 97 is stored in i.
Here, Console.Write method is used to print the value inside i, So, the output will be 97.
Example program to print a simple message using Read
Program
using System;
namespace read
{
class Program
{
static void Main(string[] args)
{
int i = Console.Read();
Console.Write("\n");
Console.Write("you entered: " + i);
Console.ReadKey();
}
}
}
Output
a
you entered: 97_
Explanation
In the above program i is a variable of int datatype which is used to store the ascii value of the data given by user in runtime using Read method. First Write method is used for new line and second Write method is used to print message "you entered: " as it is and also to concatenate the value present in the variable i as show in the output.
ReadLine
ReadLine method is under Console class used to Read data from the user given in runtime.When you read a message using ReadLine the cursor will appear in the next line.
Return type of ReadLine method is string ie., it returns back the data given by the user in the form of string.
It returns the value as it is given by the user so ReadLine method is preferred to take input data from the user in runtime.
It doesn't take any parameter between the braces.
Example
string s=Console.ReadLine();
Console.Write(s);
if the user enters a then a as it is stored in string variable s.
Here, Console.Write method is used to print the value inside s, So, the output will be a.
Example program to print a simple message using ReadLine
Program
using System;
namespace readline
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
Console.Write("you entered: " + s);
Console.ReadKey();
}
}
}
Output
a
you entered: a_
Explanation
In the above program s is a variable of string datatype which is used to store the value given by user in runtime using ReadLine method. Write method is used to print message "you entered: " as it is and also to concatenate the value present in the variable s as show in the output.
Clear
Clears the Screen.Example
Console.WriteLine("Text to clear");
Console.Clear();
Console.WriteLine("hello");
Explanation
Clears the text before clear method is executed.
Beep
Plays the sound of a beep through the console speaker.
Example
Console.Beep();
Plays one beep sound.
Example
Console.BackgroundColor = ConsoleColor.Red;
Console.Write("Before reset");
Console.ResetColor();
Console.WriteLine("After reset");
Specifies the title of the console window.
Example
Console.Title = "Title of console";
Example
Console.BackgroundColor = ConsoleColor.Red;
Console.Write("Red Background");
Example
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("Green text");
Example
Console.CursorSize = 70;
string s= Console.ReadLine();
Example program by using Console properties
Program
Example
Console.Beep();
Plays one beep sound.
Reset color
Resets the background and foreground colours to its defaults.Example
Console.BackgroundColor = ConsoleColor.Red;
Console.Write("Before reset");
Console.ResetColor();
Console.WriteLine("After reset");
Console Properties
TitleSpecifies the title of the console window.
Example
Console.Title = "Title of console";
Background color
Specifies the background color of the text.Example
Console.BackgroundColor = ConsoleColor.Red;
Console.Write("Red Background");
Foreground color
Specifies text color.Example
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("Green text");
CursorSize
Specifies the width of the blinking cursor which is used to enter text in the console window (1 to 100).Example
Console.CursorSize = 70;
string s= Console.ReadLine();
Example program by using Console properties
Program
using System;
namespace ConsoleProperties
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Text to clear");
//This clear method will clear the text before this
Console.Clear();
//Plays one beep sound
Console.Beep();
//Change bg color to Red
Console.BackgroundColor = ConsoleColor.Red;
Console.Write("Before reset");
//Reset bg color
Console.ResetColor();
Console.WriteLine("After reset");
//Change Console app title
Console.Title = "Title of console";
//Change Foreground color of text
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("Green text");
//Change cursor size
Console.CursorSize = 70;
string s = Console.ReadLine();
Console.ReadKey();
}
}
}