How to Create Console Assemblies


Console applications are the kind of executables that you would use to create a command line utility. The command line utility could be dir or xcopy. Actually many ad-hoc C# projects are console applications. The C# source code shown in Figure 1 will calculate how many rabbits you get after a certain number of generations (assuming ideal circumstances, of course).

Figure 1 above is an example of a console application. If you build this application you can run it from the command line, and pass it an argument indicating the number of generations you would like to calculate.
Building a Console Application File
To build this file using the command line compiler you would use the following command.
C:\>csc Rabbit.cs
You will notice that it is not necessary to use the /Target switch like it was when we built the source code. This is because the default assembly built by the C# compiler is a console executable.This is how you build a console application; simple stuff!
However, the Rabbits.cs sample also shows some interesting things about C# in general, so we will highlight them hereLike our last application, Rabbits.cs defines a class (arbitrarily named App) which defines an entry point function named Main().
The Main() method in Rabbits.cs does something a little different by taking a parameter defined as an array of String objects. These are the command line arguments passed to the program.
Rabbits.cs uses structured exception handling to handle errors. This is the try-catch code block that surrounds the bulk of the application. You will notice from this sample that C# uses C syntax for its loop constructs. The while loop syntax in Rabbits.cs, is exactly what it would be in C, C++, or Java.
The sample code in Figure 1 uses the static WriteLine() method of the Console type defined in the Framework Class Library to output text to the console window. Notice that an instance of the Console type was not necessary to call the method. This is because WriteLine() is defined as a static method.
This sample uses a numeric type called Decimal which allows us to calculate many more generations than we would be able to do with a 32-bit integer value. The Decimal type is well suited for financial and scientific applications.
Rabbits.cs is an iterative Fibonacci calculator. Its’ written this way just for fun. There is nothing about C# that would prohibit you from implementing this algorithm using recursion.
Building and Running a Console Application
1. The source code in Figure 1 Rabbits.cs is a console Fibonacci number generator written in C#.
2. Type in or copy the source code and save it in a .cs file.
3. Compile the source code using the command line compiler.
a. Hint: The line that you would use to compile this application is as follows.
c:\>csc Rabbit.cs
4. Run the new executable.
SUMMARY & CONCLUSION
In this treatise, we learnt that Console applications are the kind of executables that you would use to create a command line utility. The command line utility could be dir or xcopy. We equally identified the steps required to build and run a console application. We have considered the concept of console application, the types of command line utility and a classic example of a console application. We equally identified the steps required to build and run a console application.
REFERENCES
  • Microsoft. September 4, 2003."Visual C#.net Standard" (JPEG).
  • Microsoft (June 18, 2009) C# Language Reference.
    10.O' Reilly. (2002).C# Language Pocket Reference. ISBN 0-596-00429-X.
  • Petzold, Charles (2002). Programming Microsoft Windows with C#. Microsoft Press. ISBN 0-7356-1370-2.
  • Steinman, Justin (November 7, 2006). "Novell Answers Questions from the Community".
  • Stallman, Richard (June 26, 2009). "Why free software shouldn't depend on Mono or C#".
  • Simon, Raphael; Stapf, Emmanuel; Meyer, Bertrand (June 2002). "Full Eiffel on the .NET Framework".
  • "The ECMA C# and CLI Standards". 2009-07-06
  • Archer, Tom (2001). Inside C#. Microsoft Press. ISBN 0-7356-1288-9.
  • Cornelius, Barry (December 1, 2005). "Java 5 catches up with C#". University of Oxford Computing Services.
  • Ecma International. June 2006.C# Language Specification (4th ed.).
    Guthrie, Scott (November 28, 2006). "What language was ASP.Net originally written in?"
  • Hamilton, Naomi (October 1, 2008). "The A-Z of Programming Languages: C#". Computerworld.
  • Horton, Anson (2006-09-11). "C# XML documentation comments FAQ".
  • Kovacs, James (September 07, 2007). "C#/.NET History Lesson".

No comments:

Post a Comment