📒
SDV503
  • SDV503
  • Command Prompt
    • Windows Command Prompt
    • Mac OS Terminal
  • GIT & GITHub
    • GitHub
    • Git
      • Git Workflow
        • Forking Workflow
  • README
    • How to write a readme.md file?
    • Write a better README.me
    • Generate a README for GitHub Repo Quickly
  • Code-comments
    • Clean Comments
    • Writing Comments in a Good way
  • Pair Coding
    • What is Pair Coding?
    • Pair Programming Experience
  • Programming?
    • What is Programming?
    • What Is A Programming Paradigm?
    • JavaScript Programming Paradigms
  • Number Systems
    • Decimal and Binary numbers
  • JavaSCript
    • Introduction To JavaScript
      • The JavaScript Engine
  • JS Call Stack
    • JavaScript call stack
    • JavaScript & Memory
      • Memory Leaks in JavaScript
    • Execution Context and Execution Stack in Javascript
      • Javascript Execution Context and Hoisting
  • JavaScript Variables
    • Introduction to JS Data Types
    • Primitive and Non-Primitive
    • Operator precedence and associativity
      • JS Operators Part One
      • JS Operators Part Two
      • JS Operators Part Three
    • Clean Code for JS Variables
  • JavaScript Scopes
    • Scope (Chain) Visualized
  • Javascript  — this keyword
  • JavaScript Data Types
    • More JavaScript Data Types
  • JavaScript Expressions and Statements
  • if/else, switch and ternary operator
    • If/Else statement
  • Switch Statement
  • Ternary Operator
  • JavaScript Loops
    • Loops in JavaScript
      • Trouble With Loops
  • Objects
    • JavaScript Objects
      • Prototypal Inheritance Visualized
      • JavaScript Number Object
      • JavaScript String Object
  • Functions
    • JavaScript Function Part One
    • JavaScript Function Part Two
    • Immediately Invoked Function Expressions ~ IIFE
    • JS => Arrow Functions
    • JavaScript Callback
    • Hoisting in JavaScript
      • Hoisting Visualized
    • Recursion Functions
    • Curry and Function Composition
  • JavaScript Features
    • JSpread Operator
    • JavaScript Built-in Functions & Objects
  • Data Structures&Algorithms
    • JavaScript’s Data Types
    • Data Structures in JavaScript
      • Introduction to Big O Notation
      • Big O Notation in Javascript
      • Linked Lists
        • Linked Lists — 2
      • Hash Tables
      • Stack & Queue
  • TLDR
    • Single quotes (‘ ’) and double quotes (“ ”) in JavaScript
  • ES6
    • Generators and Iterators
    • Javascript Classes
    • JavaScript closures
    • JavaScript Promises & Async/Await
      • Event Loop Visualized
  • C#
    • What does C#? (C Sharp)
    • C# vs JavaScript
    • What Is The Difference Between C#, .NET, ASP.NET, Microsoft.NET, and Visual Studio?
    • What is the .NET Framework?
    • Methods and Properties of Console Class in C#
    • Datatypes in C#
    • C# Code Comments
    • The if statement
    • The switch statement
    • Loops
    • Comparison operators
    • Addition assignment operators
    • The String Interpolation Operator
    • Arrays
    • Lists
    • Dictionaries
Powered by GitBook
On this page

Was this helpful?

  1. C#

Methods and Properties of Console Class in C#

  1. What is Console Class in C#?

  2. Properties of Console Class in C#.

  3. Methods of Console class in C#.

  4. Understanding the use of Write and WriteLine method in C#.

  5. Program to show how to print the value of a variable in a console application.

  6. Understanding the use of the ReadLine method in C#.

  7. 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.

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("HELLO");
            Console.Write("WELCOME");
            Console.ReadKey();
        }
    }
}

Example: Program to show how to print the value of a variable in a console application.

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "pranaya";
            Console.WriteLine(name);
            Console.Write("hello " + name);
            Console.ReadKey();
        }
    }
}

Example: Program to show how to read the value at runtime in a console application.

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            Console.WriteLine("ENTER TWO NUMBER");
            a = int.Parse(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = a + b;
            Console.WriteLine("THE SUM IS :" + c);
            Console.WriteLine("THE SUM IS : " + (a + b));
            Console.ReadKey();
        }
    }
}

Example: Program to take two numbers as input from the console and then print the summation in the console.

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            Console.WriteLine("ENTER TWO NUMBER");
            a = int.Parse(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = a + b;
            Console.WriteLine("THE SUM IS :" + c);
            Console.WriteLine("THE SUM IS : " + (a + b));
            Console.ReadKey();
        }
    }
}

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.

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int EID, SALARY;
            string ENAME, ADDRESS, JOB;

            Console.WriteLine("ENTER THE EMPLOYEE DTAILS");

            Console.WriteLine("ENTER THE EMPLOYEE ID");
            EID = int.Parse(Console.ReadLine());

            Console.WriteLine("ENTER THE EMPLOYEE NAME");
            ENAME = Console.ReadLine();

            Console.WriteLine("ENTER THE EMPLOYEE SALARY");
            SALARY = int.Parse(Console.ReadLine());

            Console.WriteLine("ENTER THE EMPLOYEE ADDRESS ");
            ADDRESS = Console.ReadLine();

            Console.WriteLine("ENTER THE EMPLOYEE JOB");
            JOB = Console.ReadLine();

            Console.WriteLine("\n\n\nTHE EMPLOYEE DETAILS ARE GIVEN BELOW :");
            Console.WriteLine("THE EMPLOYEE ID IS: " + EID);
            Console.WriteLine("THE EMPLOYEE NAME IS: " + ENAME);
            Console.WriteLine("THE EMPLOYEE SALARY IS: " + SALARY);
            Console.WriteLine("THE EMPLOYEE ADDRESS IS: " + ADDRESS);
            Console.WriteLine("THE EMPLOYEE JOB IS: " + JOB);
            Console.ReadKey();
        }
    }
}

Example:

Program to accept student no, student name, mark1, mark2, mark3 and calculate the total mark and average marks and printing accepted information.

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int SNO, MARK1, MARK2, MARK3, TOTAL, AVERAGE;
            string SNAME;

            Console.WriteLine("ENTER THE STUDENT DETAILS");

            Console.WriteLine("ENTER THE STUDENT NO");
            SNO = int.Parse(Console.ReadLine());

            Console.WriteLine("ENTER THE STUDENT NAME");
            SNAME = Console.ReadLine();

            Console.WriteLine("ENTER THE MARKS OF 3 SUBJECTS");
            MARK1 = int.Parse(Console.ReadLine());
            MARK2 = int.Parse(Console.ReadLine());
            MARK3 = int.Parse(Console.ReadLine());

            TOTAL = MARK1 + MARK2 + MARK3;
            AVERAGE = TOTAL / 3;

            Console.WriteLine("\n\n\nTHE STUDENT DETAILS ARE GIVEN BELOW :");
            Console.WriteLine("THE STUDENT NO IS: " + SNO);
            Console.WriteLine("THE STUUDENT NAME IS: " + SNAME);
            Console.WriteLine("TOTAL MARKS IS : " + TOTAL);
            Console.WriteLine("AVEARGE MAARK IS: " + AVERAGE);

            Console.ReadKey();
        }
    }
}

Example:

Program to show the use of BackgroundColor, ForegroundColor and Title properties of Console class in C#.

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.ForegroundColor = ConsoleColor.White;
            Console.Title = "Understanding Console Class";

            Console.WriteLine("BackgroundColor Blue");
            Console.WriteLine("ForegroundColor White");
            Console.WriteLine("Title Understanding Console Class");
            Console.ReadKey();
        }
    }
}

PreviousWhat is the .NET Framework?NextDatatypes in C#

Last updated 4 years ago

Was this helpful?