Brief analysis of System.Console.WriteLine ()
The function of the Writeline () function writes a specified string and a line of characters to the StreamWriter class , with a total of 19 overloads, the main difference from the write () function is that it wraps the string after it has been output. and write () does not break the line. Now, through the generic version of WriteLine () (i.e. WriteLine (string format,object arg0,object arg1,object arg2,object arg3,__arglist)) To get a deeper understanding of its implementation details.
First, let's take a look at the source code:
[CLSCompliant (False), HostProtection (SecurityAction. LinkDemand, ui=true) public static void WriteLine(string format, object arg0, object arg1, Object arg2, Object Arg3, __arglist) { ArgIterator Iterator = new ArgIterator (__arglist); int num = iterator. Getremainingcount () + 4; Number of parameters object[] arg = new Object[num]; Parameter collection Arg[0] = arg0; ARG[1] = arg1; ARG[2] = arg2; ARG[3] = Arg3; for (int i = 4; i < num; i++) { Arg[i] = Typedreference.toobject (iterator. Getnextarg ()); } Out.writeline (format, ARG); } |
Looking at the function signature, WriteLine () is a static function with variable parameters without a return value. and output a string by textwrite a static object (out) method (Out.writeline ()). (Note: Textwrite is an abstract class that cannot be instantiated, so out is actually a reference to an instance of the Streamwrite class) |
[__dynamicallyinvokable] Public virtual void WriteLine(string format, params object[] arg) { This. WriteLine (String. Format (this. formatprovider, format, ARG); } |
The code is short, which is to call another overload of the same object's WriteLine (), and use a method of the string class to replace the corresponding placeholder in the string with a parameter to get the complete string to output. |
[__dynamicallyinvokable] Public virtual void WriteLine(string value) { if (value = = null) { This. WriteLine (); } Else { int Length = value. Length; int num2 = this. Corenewline.length; Corenewline default value is "\ r \ n" char[] Destination = new Char[length + num2]; String containing the newline character (destination string) Value. CopyTo (0, destination, 0, length); Switch (NUM2)//Determines whether the value of Corenewline is "\ n" (num2==1) or "\ r \ n" (num2==2) { Case 2: Destination[length] = this. CORENEWLINE[0]; Destination[length + 1] = this. CORENEWLINE[1]; Break Case 1: Destination[length] = this. CORENEWLINE[0]; Break Default Buffer.internalblockcopy (this. Corenewline, 0, destination, length * 2, num2 * 2); Break } This. Write (destination, 0, length + num2); } } |
Handles the newline character and adds it to the string, using an overloaded output of write (). |
|
[__dynamicallyinvokable] Public virtual void Write(char[] buffer, int index, int count) { if (buffer = = NULL) { throw new ArgumentNullException ("Buffer", environment.getresourcestring ("Argumentnull_buffer")); } if (Index < 0) { throw new ArgumentOutOfRangeException ("Index", environment.getresourcestring ("Argumentoutofrange_neednonnegnum")) ; } if (Count < 0) { throw new ArgumentOutOfRangeException ("Count", Environment.getresourcestring ("Argumentoutofrange_neednonnegnum")) ; } if ((buffer. Length-index) < count) { throw new ArgumentException (environment.getresourcestring ("Argument_invalidofflen")); } for (int i = 0; i < count; i++) { This. Write (Buffer[index + i]); } } |
Check that the string has an exception starting with the first character, and only call write () to output the final string (one character output) after it is completely no exception. |
[__dynamicallyinvokable]
Write (char value)
{
This. Checkasynctaskinprogress (); Asynchronous operation
if (This.charpos = = This.charlen)
{
This. Flush (False, false);
}
This.charbuffer[this.charpos] = value;
this.charpos++;
if (This.autoflush)
{
This. Flush (True, false);
}
}
|
Note: The TextWriter class does not implement this method, and the out object calls an instance of StreamWriter, and StreamWriter inherits from TextWriter and implements this method. |
|
The function of System.Console.WriteLine () is to output a character or string and wrap it, its implementation takes into account all aspects, including the refinement of functions, the precise organization, the distinct level and so on.
We program, not necessarily to others such a class, a method of writing so organized, but should learn the ideas of other people's programming and code structure and organization method, and try to match others,
This helps us to build a clear code architecture and structure for programming, which improves programming efficiency and learning efficiency.
Brief analysis of System.Console.WriteLine ()