Diagram of C # console output and console related programming review summary

Source: Internet
Author: User
Tags mscorlib set background sharpdevelop

1 Basic Console output

First enter the SharpDevelop, create a new control station project;

SharpDevelop Introduction:

http://blog.csdn.net/bcbobo21cn/article/details/44200205



Using system;namespace conwrdemo{class program{public static void Main (string[] args) {Console.WriteLine ("Hello world!"); /todo:implement functionality Hereconsole.write ("Press any key to continue ... "); Console.readkey (True);}}}

Results


The difference between 2 write () and WriteLine () is that both write () and WriteLine () are System.Console provided methods, both of which are used primarily to set the output stream by the specified output device (default is the screen) displayed. Two the difference in the Console.WriteLine () method is to output the string to be output along with the newline control character, and when the second statement finishes executing, the cursor moves to the next line of the current output string. As for the Console.Write () method, The cursor stops at the last character of the output string and is not moved to the next line.

3 output format a detailed format item takes the following form:
{index[,alignment][:formatstring]}

Where "index" means index placeholder;
", alignment" alignment, with "," for the mark;
": FormatString" is the qualification for the output format, marked with ":".

Alignment: Optional, is a signed integer that indicates the preferred format field width. If the alignment value is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. If align is a positive number, the field's formatted data is right-aligned, and if align is negative, the field's formatted data is left-aligned. If padding is required, use white space. If you specify alignment, you need to use commas.

FormatString: consists of standard or custom format specifiers.


Using system;namespace conwrdemo2{class program{public static void Main (string[] args) {Console.WriteLine ("Output from various data formats:"     )///Console.WriteLine Output Console.WriteLine ("{0, 8:c}", 2) in various data formats;    $2.00 Console.WriteLine ("{0, 8:c3}", 2);       $2.000 Console.WriteLine ("{0:D 3}", 2);        002 Console.WriteLine ("{0:e}", 2);        2.000000E+000 Console.WriteLine ("{0:g}", 2);    2 Console.WriteLine ("{0:n}", 2500000.00);      2,500,00.00 Console.WriteLine ("{0:x4}", 12);    000c Console.WriteLine ("{0, 2:x}", 12);   C Console.WriteLine ("{0:000.000}", 12.23);    012.230 Console.WriteLine ("{0:r}", 15.62);    15.62 Console.WriteLine ("{0:d}", System.DateTime.Now);    2012-3-27 Console.WriteLine ("{0:D}", System.DateTime.Now);    March 27, 2012 Console.WriteLine ("{0:t}", System.DateTime.Now); 11:43 CoNsole.    WriteLine ("{0:t}", System.DateTime.Now);    11:43:34 Console.WriteLine ("{0:f}", System.DateTime.Now);    March 27, 2012 11:43 Console.WriteLine ("{0:f}", System.DateTime.Now);    March 27, 2012 11:43:34 Console.WriteLine ("{0:g}", System.DateTime.Now);    2012-3-27 11:43 Console.WriteLine ("{0:g}", System.DateTime.Now);    2012-3-27 11:43:34 Console.WriteLine ("{0:m}", System.DateTime.Now); March 27 Console.WriteLine ("{0:r}", System.DateTime.Now);//Tue, Mar 11:43:34 GMT CONSOLE.W    Riteline ("{0:s}", System.DateTime.Now);    2012-03-27t11:43:34 Console.WriteLine ("{0:u}", System.DateTime.Now);    2012-03-27 11:43:34z Console.WriteLine ("{0:u}", System.DateTime.Now);    March 27, 2012 3:43:34 Console.WriteLine ("{0:y}", System.DateTime.Now);   March 2012 Console.WriteLine ("{0:d D}", System.DateTime.Now); Console.wrIteline ("{0:d dd}", System.DateTime.Now); Two Console.WriteLine ("{0:d ddd}", System.DateTime.Now);    Tuesday Console.WriteLine ("{0:f}", System.DateTime.Now);   March 27, 2012 11:46 Console.WriteLine ("{0:ff}", System.DateTime.Now);  Console.WriteLine ("{0:fff}", System.DateTime.Now); 187 Console.WriteLine ("{0:ffff}", System.DateTime.Now); 1875 Console.WriteLine ("{0:fffff}", System.DateTime.Now);   18750 Console.WriteLine ("{0:gg}", System.DateTime.Now);  Ad Console.WriteLine ("{0:GGG}", System.DateTime.Now); Ad Console.WriteLine ("{0:GGGG}", System.DateTime.Now);     Ad Console.WriteLine ("{0:GGGGG}", System.DateTime.Now);    Ad Console.WriteLine ("{0:GGGGGG}", System.DateTime.Now);   Ad Console.WriteLine ("{0:hh}", System.DateTime.Now);   Console.WriteLine ("{0:hh}", System.DateTime.Now);        11     Console.WriteLine ("{0:mm}", System.DateTime.Now);   Console.WriteLine ("{0:mm}", System.DateTime.Now);  Console.WriteLine ("{0:mmm}", System.DateTime.Now); March Console.WriteLine ("{0:mmmm}", System.DateTime.Now);   March Console.WriteLine ("{0:ss}", System.DateTime.Now);   Console.WriteLine ("{0:tt}", System.DateTime.Now);   Morning Console.WriteLine ("{0:yy}", System.DateTime.Now); Console.WriteLine ("{0:yyyy}", System.DateTime.Now);   Console.WriteLine ("{0:zz}", System.DateTime.Now);  +08 Console.WriteLine ("{0:zzz}", System.DateTime.Now);  +08:00 Console.WriteLine ("{0:hh:mm:ss}", System.DateTime.Now); 11:43:34 Console.WriteLine ("{0:d d/mm/yyyy}", System.DateTime.Now); 27-03-2012//todo:implement functionality Hereconsole.write ("Press any key to continue ... "); Console.readkey (True);}}}


The following is an example of the output format, which is from the Internet;






4 WriteLine () of various overloaded namespaces: System
Assembly: mscorlib (in mscorlib.dll)

Overloaded list
Name Description
WriteLine ()
Writes the current line terminator to the standard output stream.
WriteLine (Boolean)
Writes the text representation of the specified Boolean value, followed by the current line terminator, to the standard output stream.
WriteLine (Char)
Writes the specified Unicode character, followed by the current line terminator, value to the standard output stream.
WriteLine (char[])
Writes the specified array of Unicode characters (followed by the current line terminator) to the standard output stream.
WriteLine (char[], Int32, Int32)
Writes the specified array of Unicode characters (followed by the current line terminator) to the standard output stream.
WriteLine (Decimal)
Writes the text representation of the specified Decimal value (followed by the current line terminator) to the standard output stream.
WriteLine (Double)
Writes the text representation of the specified double-precision floating-point value, followed by the current line terminator, to the standard output stream.
WriteLine (Int32)
Writes the text representation of the specified 32-bit signed integer value (followed by the Terminator of the current line) to the standard output stream.
WriteLine (Int64)
Writes the text representation of the specified 64-bit signed integer value (followed by the Terminator of the current line) to the standard output stream.
WriteLine (Object)
Writes the text representation of the specified object, followed by the ' current line terminator, ' to the ' standard output St Ream.
WriteLine (Single)
Writes the text representation of the specified single-precision floating-point value, followed by the current line terminator, to the standard output stream.
WriteLine (String)
Writes the specified string value (followed by the current line terminator) to the standard output stream.
WriteLine (String, Object)
Writes the text representation of the specified object, followed by the ' current line terminator, ' to the ' standard output St Ream using the specified format information.
WriteLine (String, Object, Object)
Writes the text representation of the specified objects, followed by the current line terminator, to the standard output s Tream using the specified format information.
WriteLine (String, Object, object, object)
Writes the text representation of the specified object (followed by the current line terminator) to the standard output stream, using the specified format information.
WriteLine (String, Object, object, object, object)
Writes the text representation of the specified object and variable-length argument list (followed by the current line terminator) to the standard output stream, using the specified format information.
WriteLine (String, object[])
Writes the text representation of the specified array of objects (followed by the current line terminator) to the standard output stream, using the specified format information.
WriteLine (UInt32)
Writes the text representation of the specified 32-bit unsigned integer value, followed by the current line terminator, to The standard output stream.
WriteLine (UInt64)
Writes the text representation of the specified 64-bit unsigned integer value, followed by the current line terminator, to The standard output stream.


5 specific uses of the C # console class

Console.Write indicates that the string is written directly to the console, without wrapping, and continuing with the previous character write.
Console.WriteLine represents a line break after writing a string to the console.
The console.read indicates that the string is read from the console and does not wrap.
Console.ReadLine indicates that a string is wrapped after it is read from the console.
Console.readkey gets the next character or function key pressed by the user, and the pressed key is displayed in the console window.
The console.beep plays the beep tone through the console speaker.
Console.clear clears the display information for the console buffer and the corresponding console window.

Output to console
Console.WriteLine ();
Console.Write ();
Console.WriteLine (value of output);
Console.Write (value of output);
Console.WriteLine ("Output format string", variable list);
Console.Write ("Output format string", variable list);

Console.WriteLine ("The wife of {0} in Deer Ding Kee has 7", strname[0],strname[1],strname[2],strname[3]);
This method contains two parameters: the format string and the variable list. "The wife of {0} in Deer Ding Kee has {1},{2},{3} and so on 7" This is the format string, {0}, {1}, {2}, {3} is called a placeholder, represents the variable table that follows sequentially, 0 corresponds to the first variable of the variable list, 1 corresponds to the 2nd variable of the variable list, and so on,

Input from console
Console.ReadLine ();
This code returns a string data that can be assigned directly to a string variable, such as:
String Strname=console.readline ();
Sometimes you need to enter a number from the console, using the content described earlier, data conversion, such as:
int Num=int. Pares (Console.ReadLine ());
int Num=convert.toint32 (Console.ReadLine ());
The above two lines of code effect the same, you can choose any one according to their own habits.


Attention:
The input results of console.readline () and Console.read () are completely different and cannot be mixed.
Console.read (), the ASCII code that returns the first character of the value
Console.ReadLine (), the return value is a string
That is, the Read method can only read the first character, and ReadLine can read more than one character and can be read as a newline.


Console.readkey () function, read is from the console reading, key means to press the keyboard, then the combination of the meaning is to get the user press the function key displayed in the window, with the previous code in the window to pause the function, in the debug state, only press any key after the window will be closed.


console input Output example;

Using system;using system.collection.generic;using system.linq;using system.text;namespace ConsoleTest{class consoletest{static void Main (string[] args) {Console.WriteLine ("Please enter the names of two pupils"); string Name1=console.readline (); string Name2=console.readline (); Console.WriteLine ("Please input two students ' scores"); int score1=int. Parse (Console.ReadLine ()); int Score2=int. Parse (Console.ReadLine ()); Console.WriteLine ("First Student's name {0}, score {1}", Name1,score1); Console.WriteLine ("Second student's name {0}, score {1}", Name2,score2); Console.readkey ();}}}




6 Console output for debugging

Window program with Debug.WriteLine
Press F5 to debug the run
The output can be opened in the Output pane, view-window-output.

Create a new WinForm project, in the button click event input such as code;

Debug.WriteLine (STR1);

Start debugging; Click the button; the value of str1 in the Output window;


7 Changing the console window size, font color, getting line numbers
Using system;namespace conwrdemo5{class program{public static void Main (string[] args) {//Todo:implement functionality H            Ereconsole.writeline (Console.windowheight);            Console.WriteLine (Console.bufferheight);            Console.readkey ();            Console.title = "Test";//Set window title Console.windowwidth = 40;            console.windowheight=20;            Console.bufferheight = 20;            Console.WriteLine (Console.windowwidth);            Console.WriteLine (Console.windowheight);            Console.WriteLine ("---------------------");            Console.WriteLine (Console.bufferwidth);                        Console.WriteLine (Console.bufferheight); Console.Write ("Press any key to continue ... ");            Console.readkey (TRUE); Console.backgroundcolor = Consolecolor.blue; Set background color console.foregroundcolor = consolecolor.white;            Sets the foreground color, which is the font Console.WriteLine ("First line white blue."); Console.resetcolor (); Sets the foreground and background colors of the console to the default ConSole.            BackgroundColor = Consolecolor.green;            Console.foregroundcolor = Consolecolor.darkgreen;            String str = "The third line is green and dark green"; Console.WriteLine (str. PadRight (Console.bufferwidth-(str. Length% console.bufferwidth)); Sets a full row of background color console.resetcolor (); Console.Write ("Press any key to continue ... ");            Console.readkey (TRUE);//calculates the number of lines in which the current cursor is located, for the value of Console.bufferheight showcolor ();                        int m = console.cursortop;//View the current line number Console.bufferheight Console.readkey (); Console.Write ("Press any key to continue ... ");            Console.readkey (TRUE);            }//shows the background color supported in the console and the foreground color static void Showcolor () {Type type = typeof (Consolecolor);            Console.foregroundcolor = Consolecolor.white; foreach (String name in Enum.getnames (type)) {Console.backgroundcolor = (Consolecolor) enum.pars                E (type, name);            Console.WriteLine (name); } Console.BackgroundColor = Consolecolor.black; foreach (String name in Enum.getnames (type)) {Console.foregroundcolor = (Consolecolor) enum.pars                E (type, name);            Console.WriteLine (name); } foreach (String BC in Enum.getnames (type)) {Console.backgroundcolor = (Consolecolo                R) enum.parse (type, BC); foreach (String FC in Enum.getnames (type)) {Console.foregroundcolor = (Consolecolor) Enu                    M.parse (type, FC);                Console.WriteLine ("Bc=" +bc+ ", fc=" +FC);            } Console.WriteLine (); }        }}}








Console.bufferheight Properties:
The current height, in rows, of the buffer area.

8 WinForm Start the console and execute the command

Process Pro = new process ();p ro. Startinfo.filename = "cmd.exe";//pro. Startinfo.arguments = @ "C:\Windows\System32";p ro. Startinfo.useshellexecute=false;pro. Startinfo.redirectstandardinput=true;pro. Startinfo.redirectstandardoutput=true;pro. Startinfo.redirectstandarderror=true;pro. Startinfo.createnowindow=false;pro. Start ();p ro. Standardinput.writeline ("ver");//pro. Standardinput.writeline ("Exit"); string Rs=pro. Standardoutput.readtoend ();


The code below, start the console, the command is not executed; next time;



9 Another setting console font color program

Using system;namespace conwrdemo7{class program{public static void Main (string[] args) {string nl = Environment.NewLine;        string[] Colornames = Enum.getnames (typeof (Consolecolor));        Console.WriteLine ("{0}all the foreground colors on a constant black background.", NL); Console.WriteLine ("Black on black was not readable.)        {0} ", NL);            for (int x = 0; x < colornames.length; + +) {Console.Write ("{0,2}:", x);            Console.backgroundcolor = Consolecolor.black;            Console.foregroundcolor = (consolecolor) enum.parse (typeof (Consolecolor), colornames[x]);            Console.Write ("This is foreground color {0}.", Colornames[x]);            Console.resetcolor ();        Console.WriteLine ();        } Console.foregroundcolor = Consolecolor.yellow;        Console.Write ("/x01");        Console.Write ("/u0001");        Console.Write ("/001");        Console.Write ("/x10");        Console.Write ("/u0010");   Console.Write ("/020");     Console.WriteLine ();        Console.Write ("{0,-50}", "Class1.testmethod1");        Console.Write ("{0,-2}", "/x10");        Console.foregroundcolor = Consolecolor.green;        Console.WriteLine ("Pass");        Console.WriteLine ();        Console.foregroundcolor = Consolecolor.yellow;        Console.Write ("{0,-50}", "class1.testmethod2");        Console.Write ("{0,-2}", "/x10");        Console.foregroundcolor = consolecolor.red;        Console.WriteLine ("Failed"); Console.ReadLine ();}}}




The above works, altogether 7 small projects;


Http://pan.baidu.com/s/1o8qyWLs

Filename

Conwrdemo


Diagram of C # console output and console related programming review summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.