Category: C #, VS2015
Date Created: 2016-06-14
Textbook: Twelve-Five National Planning Textbook "C # Program design and Application Course" (3rd edition)
I. Summary of key points
The 1th to 6th chapter of the C # programming and Application Tutorial (3rd Edition) is the programming foundation of the C # "language" level, which is implemented using console applications (the VS2015 version of the companion source program also provides WPF's counterpart implementation for reference), but these language-level foundations also apply to other " Various types of applications, so it is important to learn what C # programming must first Master.
It's important to remember that writing programs using only console applications is never the end goal, so you'll still be hitting the wall when you're involved in a real project.
Second, the output
No matter what language you write the program in, the first thing you want to know is how I show the results and how to receive keyboard input information. Therefore, knowing how to output and input content is the first technology that must be mastered.
In the. NET Framework, console input and output are implemented by the console class under the System namespace. Because the console application template automatically adds a reference to the Systm namespace, you can call the static method provided by the System.Console class directly.
Typical usage:
Console.Write ();
Console.Write (...);
Console.WriteLine ();
Console.WriteLine (...);
For other types of applications (such as WPF applications), the console can also be used to output information, but only for the programmer to see for themselves, in order to let the programmer himself to observe the debugging information, rather than for the end user to see. Typical usage:
if DEBUG Console.WriteLine ("OK"); #endif
Note that the precompiled directive is used (advanced usage, which is not covered in the textbook). This code means: if it is a debug state, use the console to output the specified information, otherwise it will not be executed.
Another alternative to the programmer's own observation of debug information is to invoke the. WriteLine method directly under the System.Diagnostics.Debug namespace instead of the precompiled instruction, which results in the same effect as outputting debug information using precompiled directives. For example:
System.Diagnostics.Debug.WriteLine ("OK");
I prefer to use this method to achieve.
Regardless of which of the two methods described above, the function is "only in the debug state" to output the corresponding information. In other words, it executes the corresponding statement only when you press <F5> debug to run. If you are running by <Ctrl>+<F5> without debugging, or a post-release program, it will not execute these statements.
Third, input
Typical usage Examples:
string s = Console.ReadLine (); Receives 1 lines of input from the keyboard into the string s
var c = Console.readkey (); Receive 1 characters from keyboard input to C
Four, formatted output
According to the textbook examples of learning can be.
Once you have learned the basic input and output statements, then you will want to know "what to do if I want to output the specified information in some format", which is the meaning of formatted output.
Remember: Formatted output is not limited to console applications. In other words, in various types of applications, this is formatted, and Console.WriteLine (...) Just an example.
"C #" 1.2 Console Application Learning Essentials