Console programs are easy to develop. They read input. They write output.In Visual Studio,
please select New Project
and then Console Application. Many useful methods are available.
Visual Studio
Programs
are arguably the most complicated engineering artifacts ever produced;
they consist of many many details, every one of which must be correct
before the program will work completely. Aho et al., p. 23
Intro
This program is a quick introduction to the Console class. It simply prints a series of characters to the terminal window. This does not result in a world-class visual experience. It is useful for certain type of applications.Based on:
.NET 4.5
Program that uses Console type: C#
using System;
class Program
{
static void Main()
{
// Write to console window.
Console.WriteLine("Thanks for visiting!");
}
}
Write.
You will want to display information when the console program executes.
If the program takes a long time to finish, printing the status of the
operation is useful as users won't assume the program froze.WriteWriteLineSetOut
Colors:You can change the foreground color and background color of text you write to the console.
Console ColorsRead.
The Console type has many methods available to read in user input from
the terminal window. When the user types in characters, you can read
that instantly with ReadKey or when the Enter key is pressed, using
ReadLine.ReadReadKeyReadLine
Arguments.
It is useful to pass string arguments to your console programs. You can
do this by creating a shortcut in Windows to the actual executable. We
write the C# code to process the arguments.Main, ArgsArgs LoopEnvironment.GetCommandLineArgsHeight and width. You can control the height of your console window with the Console.WindowHeight property. The Console.LargestWindowHeight property can help you determine the optimum height.
WindowHeight
Also:Annoying beeps are occasionally useful in programs. We take a humorous look at the Beep method on the Console type.
BeepTitle
In
this example program, we use a while true loop and simply set the
Console.Title property to whatever the user typed into the console. When
you run this program, you can change the title of the console window by
entering text.
Tip:Console.Title allows us to change the console window title. We can use it to reduce the amount of text written to the screen.
Program that uses Title property: C#
using System;
class Program
{
static void Main()
{
while (true)
{
// Assign Console.Title property to string returned by ReadLine.
Console.Title = Console.ReadLine();
}
}
}
CapsLock
This program simply prints the value returned by Console.CapsLock every one second. When you execute it, try pressing the caps lock key. It will start printing True when the key is pressed.Sleep
Program that uses CapsLock property: C#
using System;
using System.Threading;
class Program
{
static void Main()
{
while (true)
{
Thread.Sleep(1000);
bool capsLock = Console.CapsLock;
Console.WriteLine(capsLock);
}
}
}
Output
False
False
True
True
True
False
True
True
The Console.CapsLock property
could be used in several different ways. If a program is requiring a
password to be entered, you could print an error message if caps lock is
pressed and the password is incorrect.
And:You could even add a separate "mode" in your program depending on whether caps lock is pressed.
So:With Console.CapsLock we detect whether caps lock is enabled on the computer. This property cannot be assigned.
NumberLock
This
program introduces a simple loop where the current value of the
Console.NumberLock property is printed to the console every one second.
As this program executes, you can press Num Lock and the output of the
program will change.Program that uses NumberLock: C#
using System;
using System.Threading;
class Program
{
static void Main()
{
while (true)
{
Console.WriteLine(Console.NumberLock);
Thread.Sleep(1000);
}
}
}
Output
False
False
True
True
False
False
You cannot set
the value of the NumberLock property. If you do not want the key
pressed, you must tell the user to press it again. On most keyboards, a
light will glow if the Num Lock key is pressed.
Tip:Console.NumberLock provides a way to determine if a keyboard key is pressed.
And:In some programs the Num Lock key is used to change the keyboard mode and the program's response to key presses.
Programs
Let's
explore some complete or nearly-complete program implementations. These
programs are useful for testing purposes. Long or complex programs are
not available here. No downloads are available in binary form.
Note:This site contains thousands of complete programs. Most are not useful tools for anything except demonstration.
Benchmark.
One useful program implementation is a benchmarking harness. This
program will test two pieces of C# code. It then prints results of the
time required for each piece to execute.Benchmarks
Web pages.
These programs download specified web pages. They are useful for when
you are testing a web site. The code in these programs was used
extensively in the development of this website.Decompress GZIPWebClient
Also:You can play sounds in your C# program using the SoundPlayer type. This program requires you to specify a WAV file to play.
SoundPlayerSummary
Console
programs are ideal for situations where the user interface is not
important. They provide a way to output important information. And they
do not over-complicate the software with graphical user interface code.
No comments:
Post a Comment