Interaction with Console

 What is a Console application?

A Console application in C# a command line application. It takes the input from the user and writes the output to the command line console window. The C# console application interacts with the three standard data stream i.e. standard input, standard output and standard error.

What is Console class and it's functionality in C#?

A Console class is defined in System namespace. It contains several properties and methods that are used to interact with standard input, output and error stream. It is a static class and it cannot be inherited.

Create a Console application in C#.

We will now create a console application. Before that we need to install .NET framework SDK. Some of the .NET framework SDKs comes preinstalled in windows. You can also download them separately from Microsofts website 

You can check the .NET framework version installed on your machine by following below steps.'=

  • Press Win + R.
  • Type 'cmd' without quotes and press enter.
  • on the command prompt type 'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP" '. This will show the list of SDKs installed in  your system
        



Another prerequisite is to install Visual Studio. Visual studio is an Integrated Development Environment (IDE) developed by Microsoft to develop different types of applications. Visual studio comes with 3 different editions those are Community, Professional and Enterprise. The community edition is free to use for individual purpose. You can download the community edition from Microsoft official website by clicking here. The latest version of visual studio is VS 2022.

If a system does not have .NET framework installed, visual studio downloads and installs it automatically.

  • After installation, open the visual studio.
  • Click on File menu -> New -> Project
  • A windows opens with different type of project templates.


  • Select the Console app (.NET Framework) template and click Next.
  • Provide the name and location and click Create.

  • In the method static void main(string [] args) write Console.WriteLine("Hello World");

  • Save the program using ctrl + s. Click on the Start button on the menu bar to run the program.
  • The below console window will open and show the message "Hello world".


Program to read user input and write data to the console.

using System;

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string firstName,lastName;
            Console.WriteLine("Enter first name");
            firstName = Console.ReadLine();
            Console.WriteLine("Enter last name");
            lastName = Console.ReadLine();
            Console.WriteLine("Welcome " + firstName + ' ' + lastName);
        }
    }
}


Output



Click here to watch the video tutorial.


Comments

Popular posts from this blog

Implicitly typed keyword - var in C#

Classes and objects in C#

Data types in C#