3 Surprising Things to Do with the Console in C#

The Console class can do more than just WriteLine().

Here’s 3 fun/weird/useful/annoying things.

1. Setting The Console Window Size

The Console.SetWindowSize(numberColumns, numberRows) method sets the console window size.

Console.SetWindowSize

 

To annoy your users (or create a “nice” console opening animation) as this animated GIF shows you could write something like:

for (int i = 1; i < 40; i++)
{
    Console.SetWindowSize(i,i);
    System.Threading.Thread.Sleep(50);
}

2. Beeping Consoles

The Console.Beep() method emits a beep from the speaker(s).

We can also specify a frequency and duration.

The following console application allows the performer to adjust frequency and duration by using the keyboard arrows:

internal class Program
{
    private static int _frequency = 10000;
    private static int _duration = 100;

    private static void Main(string[] args)
    {
        Console.WriteLine("Use keyboard arrows to adjust frequency and duration");
        Console.WriteLine("Press CTRL-C to quit");

        do
        {
            // While no one is pressing any keys just keep on beeping
            while (!Console.KeyAvailable)
            {
                Console.Beep(_frequency, _duration);
            }


            // A key has been pressed, read what key (and prevent echoing to console output)
            var k = Console.ReadKey(true);

            switch (k.Key)
            {
                case ConsoleKey.UpArrow:
                    IncreaseFrequency();
                    break;
                case ConsoleKey.DownArrow:
                    DecreaseFrequency();
                    break;
                case ConsoleKey.RightArrow:
                    IncreaseDuration();
                    break;
                case ConsoleKey.LeftArrow:
                    DecreaseDuration();
                    break;
            }
        } while (true);
    }

    private static void IncreaseDuration()
    {
        _duration += 100;
        _duration = Math.Min(_duration, 1000);
    }

    private static void DecreaseDuration()
    {
        _duration -= 100;
        _duration = Math.Max(_duration, 100);
    }

    private static void IncreaseFrequency()
    {
        _frequency += 100;
        _frequency = Math.Min(_frequency, 15000);
    }

    private static void DecreaseFrequency()
    {
        _frequency -= 100;
        _frequency = Math.Max(_frequency, 1000);
    }
}

3. Fun With Titles

The Title property sets what appears in the console window title.

We can create an indeterminate style progress bar in the title by using a Unicode block character.

Console.Title

 

internal class Program
{
    private static void Main(string[] args)
    {
        const int maxProgressBarLength = 10;
        const string progressBarElement = "█";

        var title = "";

        do
        {
            title += progressBarElement;

            if (title.Length > maxProgressBarLength)
            {
                title = progressBarElement;
            }

            Console.Title = title;

            Thread.Sleep(100);
        } while (true);
    }
}

For more console related tips, check out my Building .NET Console Applications in C# Pluralsight course.

Comments (7) -

  • Consoler1

    12/12/2013 4:55:18 AM | Reply

    Pity you don't mention anything about colored text and how tricky it is to get it working.
    Obtaining a console for a Windows Forms application is not an easy thing, either.
    Oh yes, and how to pipe a console to a file - anyone ?

  • joey

    12/12/2013 7:39:54 AM | Reply

    None of these are really all that surprising and for the most part are useless toys for CS Students to play around with while they are learning. none of them really have a practical purpose except for setting the console title

    colored text on the other hand is a bit tricky to get working but is a much more practical feature in console programming, see Chocolatey for an example of this

  • Jason

    12/13/2013 9:26:44 AM | Reply

    Kristian - that is the coolest thing I have ever seen, er heard Smile

Add comment

Loading