The Power and Prowess of Console Applications and Enhancing it with .NET Framework 2.0
To start learning a new programming language, following the C++ book style, the first and foremost application that we write is
cout << "Hello World";
We have been religiously inheriting this custom through Java and to C# via the essence of Console Applications.
System.out.println ("Hello World");
System.Console.WriteLine ("Hello World");
A simpler console application lays the greater foundation for a powerful and rewarding journey through the programming language. Let us in brief see about the essence of these console applications and how .NET Framework BCL richly complements console applications.
This article was actually inspired from a discussion board question where a beginner had just vehemently violated the message board netiquette about a sample code snippet which an expert had provided with great magnanimity. Fuelling it, he/she had also scrapped base about console applications. I just thought I would take this opportunity to initiate the efforts in clearing the myth clouding about the console applications.
Simpler and Stronger
- Console applications are a stronger support to even a full-fledged application frameworks. For example consider an application like 'Windows Service' project in C#. By default, you can not run a Windows Service project but you can only attach to the running process to debug the same. But there is an easy workaround using the Visual Studio IDE. With the other classes that are used in the same way, the launching class (Service.cs) can be excluded from the project while debugging and a sample (StartupConsole.cs) can be used. This can emulate threads from within a sample Console application facilitating easier peek of the startup configuration of the Windows service.
- Console applications are a very good applications to provide code snippets for articles since they are easier to visualize, compile for the casual article viewer also.
- Console applications are lighter and supported by the heart library of the framework. .NET Framework supports Console right in System namespace (rooted to the core) and Java supports in its java.lang.
- There is also a good business potential of console applications. There are still a lot of retail shops who prefer a commandline console based data entry approach for billing and other things. We can use the richness of Console applications there.
Simpler and Sweet Extensions to Console in .NET Framework 2.0
The console applications have grown more powerful at least with respect to .NET Framework 2.0. I first thought I would provide a Class documentation for the same in http://www.dotnetspider.com/ but then I chose to include it as part of this article context for better reach.
A few of the new namespaces that add color and brightness to to the otherwise dark console are:
- ConsoleColor enumeration
- CapsLock (To detect whether CAPSLock is on or off in just a jiffy)
- NumberLock (To detect whether NumberLock is on or off in just a jiffy)
- Clear (To clear the console display. Our class CLS command of DOS. No more P/Invoke :))
- A host of other Window manipulation utilities like SetWindowPosition, SetWindowSize
- ReadKey -- Equivalent to provide 'Press any key to continue in C#' without RETURN.
However, for older .NET Framework versions, I have a workaround in my other article here: http://www.codeproject.com/useritems/PressAnyKeyToContinue.asp
P.S.: A significant amount of them are also available in .NET Framework 1.1 albeit you have to delve into P/Invoke Library for the same. However, I would suggest ManiB's article on CodeProject which hides this P/Invoke management from your core application logic. Check it out here: http://www.codeproject.com/cs/library/csconsolelibrary.asp
This article is also part of my other article in Dotnetspider. You can check it out here.