First Step To Console Application Using C# PART-1

Introduction

 
Console application is the application where console  is used for user input and the application's output display. The windows command prompt is used as console in console application. We cannot use any user interface in console. Console application is a character based application. 
 

Properties, methods and events

 
Console application provides different types of properties, methods and events.
 
Properties
 
The followings properties are used in a console application,
 
BackgroundColor       : Used to get and set the background color of a console application.
BufferHeight               : Used to get and set the buffer height of a console application.
BufferWidth                : Used to get and set the buffer width of a console application.
CapsLock                   : It will return 'true' or 'false' as per the CapsLock toggle on/off.
CursorLeft                  : Used to get and set the column position of the cursor within the buffer area.
CursorSize                 : Used to get and set the height of the cursor. 
CursorTop                   : Used to get and set the row position of the cursor.
CursorVisible               : Used to get and set a value where the cursor is visible. 
Error                            : Used to get the errors.
ForegroundColor         : Used to get and set the foreground color of a console.
In                                 : Used to get the input stream.
InputEncoding             : Used to convert the corresponding character to the input character.
IsErrorRedirected        : It will return 'true' or 'false' which indicates whether the error has been redirected from the standard error stream.
IsInputRedirected        : It will return 'true' or 'false' which indicates whether the input has been redirected from the standard inputstream.
IsOutputRedirected     : It will return 'true' or 'false' which indicates whether the output has been redirected from the standard outputstream.
KeyAvailable               : It will return 'true' or 'false' which indicates whether a key press available in the input stream.
LargestWindowHeight :Used for getting the largest possible number of rows, based on the windows current resolution. 
LargestWindowWidth  : Used for getting the largest possible number of columns, based on the windows current resolution.
NumberLock                : It will return 'true' or 'false' which indicates whether the NUM LOCK keyboard toggle is turned off or on. 
Out                              : Used for standard output stream.
OutputEncoding          : Used to get and set the encoding the console uses to write output.
Title                             : Used to get and set title to display in the title bar.
TreatControlCAsInput : Returns true if 'Ctrl+c' is treated as ordinary input, else false.
WindowHeight             : Used to get and set the window height.
WindowLeft                 : Used to get and set the leftmost position of the console.
WindowTop                 : Used to get and set the top of the console.
WindowWidth              : Used to get and set the width of the console.
 
Methods
 
Following methods are used in a console application,
 
Beep()                           :Used to play a beep sound.
Clear()                           :Clears the console window. 
MoveBufferArea()         :Used to copy a specified area buffer to a specify destination area.
OpenStandardError()    :Used to acquire the standard error stream.
OpenStandardInput()    :Used to acquire the standard input stream.
OpenStandardOutput() : Used to acquire the standard output stream.
Read()                           : Used to read the next character from the input stream.
ReadKey()                     : Display the pressed key.
ReadLine()                    : Used to read the next line of character from the input stream.
ResetColor()                 : Reset the fore and background color.
SetBufferSize()             : Used to set the height and width of the screen buffer area.
SetCursorPosition()         : Used to set the cursor position.
SetError()                        : Used to set the error format for the specific textwriter object.
SetIn()                             : Used to set the in format for the specific textwriter object.
SetOut()                          : Used to set the out format for the specific textwriter object.
SetWindowPosition()      : Used to set the window position.  
SetWindowSize()            : Used to set the height and width of the console screen.
Write()                             : Used to write the value to the output stream.
WriteLine()                       : Used to write the value to output stream also provides a new line to the end of the output string.
 
Event
 
We use the below event in  console application,
 
CancelKeyPress()        : Occurs when the 'Ctrl+c' or 'Ctrl+break' key pressed. it is used to interrupt the read operation.
 
EXAMPLE
 
Here we will take a small example of a console application.
 
Follow the below steps to create a console application:
  • Open the visual studio.
  • Go to File> New> Project
  • Then choose templates.
  • Inside templates, choose console application.
  • Open the solution explorer.
  • Find the program.cs file
  • Here you need to write your code.
  • After writing your code Press 'F5' or choose 'Run' option in visual studio.
  • Then you will able to see your console application.
In the code below i will add two numbers using console application.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Addition  
  8. {  
  9.     class Program  
  10.     {  
  11.         public int AdditionOfTwoNumbers()  
  12.         {  
  13.             int number1, number2, Result;  
  14.             Console.WriteLine("please enter the number1");  
  15.             number1 = Convert.ToInt32(Console.ReadLine());  
  16.             Console.WriteLine("please enter the number2");  
  17.             number2 = Convert.ToInt32(Console.ReadLine());  
  18.             Result = number2 + number2;  
  19.             return Result;  
  20.         }  
  21.         static void Main(string[] args)  
  22.         {  
  23.             Program _objProgram = new Program();  
  24.             Console.WriteLine("Result = "+_objProgram.AdditionOfTwoNumbers());  
  25.             Console.ReadLine();  
  26.         }  
  27.     }  
  28. }  

Summary

 
In this session, I discussed about the C# console application with properties, event and methods. I hope this session will help the beginners.


Recommended Free Ebook
Similar Articles