C Sharp Programming/The .NET Framework/Console Programming

Input
Input can be gathered in a similar method to outputting data using the and  methods of that same  class:

The above program requests the user's name and displays it back. The final waits for the user to enter a key before exiting the program.

Output
The example program below shows a couple of ways to output text:

The above code displays the following text:

Hello World! This is... my first program! Goodbye World!

That text is output using the class. The statement at the top allows the compiler to find the  class without specifying the  namespace each time it is used.

The middle lines use the method, which does not automatically create a new line. To specify a new line, we can use the sequence backslash-n. If for whatever reason we wanted to really show the \n character instead, we add a second backslash. The backslash is known as the escape character in C# because it is not treated as a normal character, but allows us to encode certain special characters (like a new line character).

Error
The output is used to divert error specific messages to the console. To a novice user this may seem fairly pointless, as this achieves the same as Output (as above). If you decide to write an application that runs another application (for example a scheduler), you may wish to monitor the output of that program - more specifically, you may only wish to be notified only of the errors that occur. If you coded your program to write to the stream whenever an error occurred, you can tell your scheduler program to monitor this stream, and feedback any information that is sent to it. Instead of the Console appearing with the Error messages, your program may wish to log these to a file.

You may wish to revisit this after studying Streams and after learning about the Process class.

Command line arguments
Command line arguments are values that are passed to a console program before execution. For example, the Windows command prompt includes a command that takes two command line arguments. The first argument is the original file and the second is the location or name for the new copy. Custom console applications can have arguments as well. c sharp is object based programming language. .net framework is a Microsoft programming language is used to create web application,console application, mobile application.

If the above code is compiled to a program called username.exe, it can be executed from the command line using two arguments, e.g. "Bill" and "Gates":

C:\>username.exe Bill Gates

Notice how the method above has a string array parameter. The program assumes that there will be two arguments. That assumption makes the program unsafe. If it is run without the expected number of command line arguments, it will crash when it attempts to access the missing argument. To make the program more robust, we can check to see if the user entered all the required arguments.

Try running the program with only entering your first name or no name at all. The property returns the total number of arguments. If no arguments are given, it will return zero.

You are also able to group a single argument together by using the quote marks. This is particularly useful if you are expecting many parameters, but there is a requirement for including spaces (e.g. file locations, file names, full names etc.)

C:\> Test.exe Separate words "grouped together" 1: Separate 2: words 3: grouped together

Formatted output
and allow you to output a text string, but also allows writing a string with variable substitution.

These two functions normally have a string as the first parameter. When additional objects are added, either as parameters or as an array, the function will scan the string to substitute objects in place of tokens.

For example:

The is identified by braces, and refers to the parameter index that needs to be substituted. You may also find a format specifier within the braces, which is preceded by a colon and the specifier in the question (e.g. ).

Rounding number example
This is a small example that rounds a number to a string. It is an augmentation for the  class of C#. The result of the Round method has to be rounded to a string, as significant figures may be trailing zeros that would disappear, if a number format would be used. Here is the code and its call. You are invited to write a shorter version that gives the same result, or to correct errors!

The constant class contains repeating constants that should exist only once in the code so that to avoid inadvertant changes. (If the one constant is changed inadvertantly, it is most likely to be seen, as it is used at several locations.)

The  class is an enhancement to the   library and contains the rounding calculations.

Extensive testing of a software is crucial for qualitative code. To say that the code is tested does not give much information. The question is what is tested. Not in this case, but often it is also important to know where (in which environment) it was tested, and how - i.e. the test succession. Here is the code used to test the  class.

The results of your better code should comply with the result I got: Maths.Round('.', 0, 5) = 0.00000 Maths.Round('.', -1.40129846432482E-45, 5) = -1.4012E-45 Maths.Round('.', 1.40129846432482E-45, 5) = 1.4013E-45 Maths.Round('.', -1.999999757E-05, 5) = -1.9999E-5 Maths.Round('.', 1.999999757E-05, 5) = 2.0000E-5 Maths.Round('.', -0.0001999999757, 5) = -0.00019999 Maths.Round('.', 0.0001999999757, 5) = 0.00020000 Maths.Round('.', -0.001999999757, 5) = -0.0019999 Maths.Round('.', 0.001999999757, 5) = 0.0020000 Maths.Round('.', -0.000640589, 5) = -0.00064058 Maths.Round('.', 0.000640589, 5) = 0.00064059 Maths.Round('.', -0.339689999818802, 5) = -0.33968 Maths.Round('.', 0.339689999818802, 5) = 0.33969 Maths.Round('.', -0.34, 5) = -0.33999 Maths.Round('.', 0.34, 5) = 0.34000 Maths.Round('.', -7.07, 5) = -7.0699 Maths.Round('.', 7.07, 5) = 7.0700 Maths.Round('.', -118.188, 5) = -118.18 Maths.Round('.', 118.188, 5) = 118.19 Maths.Round('.', -118.2, 5) = -118.19 Maths.Round('.', 118.2, 5) = 118.20 Maths.Round('.', -123.405009, 5) = -123.40 Maths.Round('.', 123.405009, 5) = 123.41 Maths.Round('.', -30.7699432373047, 5) = -30.769 Maths.Round('.', 30.7699432373047, 5) = 30.770 Maths.Round('.', -130.769943237305, 5) = -130.76 Maths.Round('.', 130.769943237305, 5) = 130.77 Maths.Round('.', -540, 5) = -539.99 Maths.Round('.', 540, 5) = 540.00 Maths.Round('.', -12345, 5) = -12344 Maths.Round('.', 12345, 5) = 12345 Maths.Round('.', -123456, 5) = -123450 Maths.Round('.', 123456, 5) = 123460 Maths.Round('.', -540911, 5) = -540900 Maths.Round('.', 540911, 5) = 540910 Maths.Round('.', -9.22337203685478E+56, 5) = -9.2233E56 Maths.Round('.', 9.22337203685478E+56, 5) = 9.2234E56

If you are interested in a comparison with C++, please compare it with the same example there. If you want to compare C# with Java, take a look at the rounding number example there.