C# Console Application Example

Since all the functionalities related to performing different types of Input/Output Operations in C# are handled by a class named Console of the System Namespace.

So before we go ahead, it will be useful for us to understand some of the methods related to Input and Output properly so that we can easily understand the different types of C# Programming Concepts using Input/Output Related Functionalities.

Defined System. Console Class is also a very big class in .NET Framework, in which many Data Members and Methods related to Console IO have been defined, and by using these Properties and Methods, we can develop a Good Looking and Powerful Console Application. Can.

In this Console Class of C#, three methods have been defined for reading data and 2 for writing, which is as follows:

Read() ReadKey() ReadLine() 

Write() WriteLine()

In this class, only the ReadKey() method related to Input is overridden, while both Write() and WriteLine() methods related to Output are overridden in many ways to different output types of data.

To get detailed information about the various Assemblies of the .NET Framework and the various Namespaces and types defined in each Namespace, compiled in those Assemblies, we can use the Object Browser Utility of Visual Studio or C# Express Edition, which can be found in the View menu of the IDE. The option named “Object Browser” can be activated by clicking on it.

As we can understand from the following picture that on Left Side, we are seeing Exist different Types in the Currently Selected mscorlib.dll Assembly, while on Left Side, we can see a list of all Members of the Currently Selected Console Class on the Right Side.

Also, in the list of Members visible on this right side, the member we select, the specific details related to that member such as Syntax, Summary, Return Value, and Exceptions, etc.


As we said that there are many overloaded variations of both Write() and WriteLine() methods, which we can see by Object Browser as shown in the above picture. But still, there is only one basic difference between these two methods, and that is that C# Compiler does not move the Cursor to New Line after the method is executed using Write() method whereas when using WriteLine() method. After the method is executed, the C# compiler does not move the CursorCursor to the new line.

That is, the Write() method does not automatically take New Line, but if New Line is needed while showing Output, then we have to use “\n” Backslash Character Constant to get New Line. While using the WriteLine() method, as soon as this method is executed, the content written after this method is displayed in the next line in the Output.

Similarly, if we want to read only one character from the keyboard, then we use Read() or ReadKey() method. But if we want to read the entire line from the keyboard, i.e., until the user presses the Enter key, then we use the ReadLine() method. Let us try to understand the working and use of these two methods by another example program.

// File Name: ConsoleReadWrite.cs
using System; 

namespace CSharpFundamentals{
class ConsoleReadWrite{
private static void Main(string[] args)
{
Console.Write("Enter your Name:");
Console.WriteLine("Hello! {0}. How are you?", Console.ReadLine());
}
}
}
// Output:
Enter your Name:Kuldeep
Hello! Kuldeep. How are you?


When this program is compiled and run, the CLR first reaches the Main() method, where it gets the following statement:

Console.Write("Enter your Name:");

In this statement, we have used the Write() method, so after this method is executed, a message is displayed on the screen as follows:

Enter your Name:

But the CursorCursor does not move to the next line. Rather, as soon as the code of this line is executed, immediately the code of the next line is executed as follows:

    Console.WriteLine("Hello! {0}. How are you?", Console.ReadLine());

In this code line, we have called Console.ReadLine() method inside WriteLine() method. Since the priority of Inner Parenthesis is always the highest in any programming language, the following code is executed first in this code:

    Console.ReadLine()

This code reads the characters to be typed from the CRL keyboard as soon as it is executed until the user presses the “Enter Key” of the keyboard. As soon as the user presses the “Enter Key” located on the keyboard, this method terminates, and all the input characters are stored in a temporary memory of the computer, which is available at the current position in the WriteLine() method. Huh.

For example, if we input the name “Kuldeep” from the keyboard, as shown in the Output of the previous program, then this name is available at the position of the Console.ReadLine() method in our previous statement as follows:

    Console.WriteLine("Hello! {0}. How are you?", "Kuldeep" );

C# provides us the facility to specify the position through Numerical Numbers, where we want to place the input string from the keyboard.

In the above code line, we want to display the string to be read by the ReadLine() method at the {0} position. So when the Console.WriteLine() statement is executed, we get the Output as follows:

Hello! Kuldeep. How are you?

Where we can see that the name inputted from the keyboard is exactly at the same position where {0} Numerical Position was specified.

In the WriteLine() method, we can specify as many Numerical Positions as we want; while specifying more than one Numerical Position, we have to specify only Numerical Numbers in the Incremented Form.

// File Name: ConsoleWriteLineMultipleArguments.cs
using System; 

namespace CSharpFundamentals
{
class ConsoleWriteMultipleArguments
{
private static void Main(string[] args)
{
Console.WriteLine("Hello! {0}. Next year you would be {1} year old.", "Kuldeep", 32);
}
}
} 

// Output:
Hello! Kuldeep. Next year you would be 32 years old.


In this example program, we have defined two Numerical Positions in the Console.WriteLine() method, where the first Argument Value at the {0} position becomes the “Kuldeep” Place, while the Second Argument Value at the {1} Position becomes the 32nd Place. Is. As a result, we get the Output as above.

If we want, we can also repeat the same placeholder number multiple times in the Console.WriteLine() method. like:

// File Name: ConsoleWriteLineSamePlaceholderNTimes.cs
using System;
namespace CSharpFundamentals
{
class ConsoleWriteLineSamePlaceholderNTimes
{
private static void Main(string[] args)
{
Console.WriteLine("Number {1}, Number {0}, Number {0}, Number {1}, ", 10, 100);
}
}
} 

// Output:
Number 100, Number 10, Number 10, Number 100


As we can understand by this Output that we can use the same Placeholder multiple times in WriteLine() method, as done in the above program. Whereas on repeating the Placeholder, we do not need to repeat the Associated Values ​​from them. Also, Placeholder {0} always represents the first parameter, even if we have specified it at any place in the Console.WriteLine() method.

Another feature of this System.Console.WriteLine() method of C# is that we can use it exactly like the System.out.println() method of Java. For example, if we want, we can also create the ConsoleReadWrite.cs program in the following way:

// File Name: UsingConsoleWriteLineLikeJavaPrintLn.cs
using System; 
namespace CSharpFundamentals
{
class UsingConsoleWriteLineLikeJavaPrintLn
{
private static void Main(string[] args)
{
Console.Write("Enter your Name:");
Console.WriteLine("Hello!" + Console.ReadLine() + ". How are you?");
}
}
} 

// Output:
Enter your Name:Kuldeep
Hello! Kuldeep. How are you?


And as we can understand from the Output of this program that the Output of both the programs is exactly the same, which is an indication that we can use the System.Console.WriteLine() Method as Exactly Java’s System. out.println() Method. can be used as

Format String
With the WriteLine() method, we can display the Output in a better way by specifying a variety of format parameters, and to do this, we have to use the Format String, whose general form is as follows:

    {argument-number, width: format}

By specifying the 0 based number as argument-number, we decide which position’s argument’s value is to be displayed at the current position. Whereas in the form of width argument, we specify the minimum width of the displayed field, and as the format, we specify the format value according to which we want to display the Output.

Both width and format parameters are optional. So in previous programs, we have used it in the format of {0}, {1}. Whereas if we modify the previous program itself and specify it as follows:

// File Name: ConsoleWriteLineFormattedOutput.cs
using System; 
namespace CSharpFundamentals
{
class ConsoleWriteLineFormattedOutput
{
private static void Main(string[] args)
{
Console.WriteLine("Name:{0, 20} \nEMail: {1, 20}", "Rajesh", "[email protected]");
}
}
} 

// Output:
Name: Rajesh
Email: [email protected]


When we specify the Format String in the WriteLine() method like this, the specified parameters in the method reserve a space of 20 characters before displaying, and then the content display starts from the right side.

Like any other programming language, C# also has its own fundamental programming constructs, which are being discussed in the next few sections. These Fundamental Programming Constructs are almost completely similar to C/C++ and Java. So if you know any of these three programming languages ​​properly, then you will not take any time to understand these concepts.

Read also: C Sharp Basics – C# Sample Program

Leave a Comment