TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Console Programs

These C# examples use the Console type. They cover Console methods, including Write and WriteLine.

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

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.

Note: The Console class is static. We do not instantiate it. Rather we just access members on it.

Based on:

.NET 4.5

C# program that uses Console type

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 Colors

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

Also: Annoying beeps are occasionally useful in programs. We take a humorous look at the Beep method on the Console type.

Beep

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

C# program that uses Title property

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

C# program that uses CapsLock property

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.

C# program that uses NumberLock

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.

Height, width. With WindowWidth, we control the width of a window based on the number of text columns. Here, 40 means the console program can display lines of 40 characters. Height changes the window size based on lines of text.

WindowHeight: With WindowHeight and its companion property LargestWindowHeight, we gain control over a window's height.

Console.WindowHeight

C# program that sets height, width

using System;

class Program
{
    static void Main()
    {
	// ... Width is the number of columns of text.
	Console.WindowWidth = 40;
	// ... Height is the number of lines of text.
	Console.WindowHeight = 10;
	// ... Say hello.
	Console.WriteLine("Hi");
    }
}

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.

SoundPlayer

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


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf