As you can see in below table the Console class does provide some members that can spice up a simple command-line application, such as the ability to change background and foreground colors and issue beep noises in a variety of frequencies.
Members of System.Console
| Member | Meaning |
|---|---|
| Beep() | This method forces the console to emit a beep of a specified frequency and duration. |
| BackgroundColor,ForegroundColor | These properties set the background/foreground colors for the current output.They may be assigned any member of the ConsoleColor enumeration. |
| BufferHeight,BufferWidth | These properties control the height/width of the console’s buffer area |
| Title | This property gets or sets the title of the current console. |
| WindowHeight, WindowWidth, WindowTop, WindowLeft | These properties control the dimensions of the console in relation to the established buffer. |
| Clear() | This method clears the established buffer and console display area. |
Basic Input-Output
In addition to the members in above Table, the Console type defines a set of methods to capture input and output, all of which are static and are, therefore, called by prefixing the name of the class (Console) to the method name. As you know, WriteLine() pumps a text string (including a carriage return) to the output stream. The Write() method pumps text to the output stream without a carriage return. ReadLine() allows you to receive information from the input stream up until the Enter key is pressed, while Read() is used to capture a single character from the input stream.Create a new Console Application project to understand basic I/O using the Console class and update your Main() method to call a helper method named GetInfo().Implement this method within the Program class with logic that prompts the user for some bits of information and echoes each item to the standard output stream. For example, you could ask the user for a name and age which will be treated as a text value for simplicity, rather than the expected numerical value for now.
Program
using System;
public class ConsoleApp
{
public static void Main(string[] args)
{
GetInfo();
Console.ReadLine();
}
static void GetInfo()
{
//read the data and store it in variable age and name
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.Write("Enter your age: ");
string age = Console.ReadLine();
//chnages the console color
ConsoleColor PrevColor = Console.BackgroundColor;
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("Name: {0}", name);
Console.WriteLine("Age: {0}", age);
//changes the console color again
Console.BackgroundColor = ConsoleColor.Black;
}
}
Output
Enter your name: Hansraj
Enter your age: 20
Name: Hansraj
Age: 20
Visual Studio supports a number of “code snippets” that will insert code once activated. The cw code
snippet is quite useful. In that it will automatically expand to Console.
WriteLine()! To test this for yourself, type in cw somewhere within your Main() method and hit the Tab key
twice. sadly, there is no code snippet for Console.ReadLine()). To see all code snippets, right-click in a C# code file and choose the Insert Snippet menu option.
Formatting Console output
You might have noticed numerous occurrences of tokens such as {0} and {1} embedded within various string literals. The .NET platform supports a style of string formatting slightly akin to the printf() statement of C. Simply put, when you are defining a string literal that contains segments of data whose value is not known until runtime, you are able to specify a placeholder within the literal using this curly-bracket syntax. At runtime, the values passed into Console.WriteLine() are substituted for each placeholder. The first parameter to WriteLine() represents a string literal that contains optional placeholders designated by {0}, {1}, {2}, and so forth. Be aware that the first ordinal number of a curly-bracket placeholder always begins with 0. The remaining parameters to WriteLine() are simply the values to be inserted into the respective placeholders.If you have more uniquely numbered curly-bracket placeholders than fill arguments, you will receive a format exception at runtime. However, if you have more fill arguments than placeholders, the unused fill arguments are ignored.
It is permissible for a given placeholder to repeat within a given string. For example
Program
using System;
public class ConsoleApp
{
public static void Main(string[] args)
{
Console.WriteLine("{0}? I love {0}, I love to code in {0}", ".NET");
Console.ReadLine();
}
}
Output
.NET? I love .NET, I love to code in .NET
Also, know that it is possible to position each placeholder in any location within a string literal, and it
need not follow an increasing sequence. For example,
Program
using System;
public class ConsoleApp
{
public static void Main(string[] args)
{
Console.WriteLine("I am {1}.My age is {0}",20,"Hansraj");
Console.ReadLine();
}
}
Output
I am Hansraj.My age is 20
This comment has been removed by the author.
ReplyDelete