浅析System.Console.WriteLine()
浅析System.Console.WriteLine()
Writeline()函数的功能向 StreamWriter 类写入指定字符串和一行字符,共有19个重载,其与Write()函数的主要区别在于它输出字符串后换行,而Write()不换行。现在,通过WriteLine()的通用版本(即WriteLine(string format,object arg0,object arg1,object arg2,object arg3,__arglist))来深入理解其实现细节。
首先,我们来看一下源代码:
|
[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; //参数个数 object[] arg = new object[num]; //参数集合 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); } |
|
看函数签名可知,WriteLine()是一个静态的无返回值的有可变参数的函数。并通过TextWrite的一个静态对象(Out)的方法(Out.WriteLine())输出字符串。 (注:TextWrite是一个抽象类,无法实例化,所以Out实际上是StreamWrite类的一个实例的引用) |
|
[__DynamicallyInvokable] public virtual void WriteLine(string format, params object[] arg) { this.WriteLine(string.Format(this.FormatProvider, format, arg)); } |
|
代码很简短,就是调用同一个对象的WriteLine() 的另一个重载,并使用String类的一个方法用参数替换字符串中相应的占位符,得到要输出的完整的字符串。 |
|
[__DynamicallyInvokable] public virtual void WriteLine(string value) { if (value == null) { this.WriteLine(); } else { int length = value.Length; int num2 = this.CoreNewLine.Length; //CoreNewLine默认值为"\r\n" char[] destination = new char[length + num2]; //包含换行符的字符串(目的字符串) value.CopyTo(0, destination, 0, length); switch (num2) //确定CoreNewLine的值是 "\n"(num2==1) 还是 "\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); } } |
|
处理换行符并添加到字符串中,用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]); } } |
|
从第一个字符开始检查字符串是否有异常,只有完全没异常后才调用Write()将最终的字符串输出(一个字符一个字符的输出)。 |
|
System.Console.WriteLine()的功能是输出一个字符或字符串并换行,其实现考虑到了各个方面,包括功能的细化,精密的组织,鲜明的层次等。
我们编程,不一定要向别人这样将一个类,一个方法写的这样有条理,但应当学习别人的编程的思想与代码的结构与组织方法等,并尽量向别人看齐,
这样,有助于我们在编程的时候构建一个清晰的代码体系和结构,提高编程效率与学习效率。
浅析System.Console.WriteLine()的更多相关文章
- System.Console.WriteLine() 调用原理
1.System.Console.WriteLine(类的实例)默认调用类的Tostring()方法.如果自定义的新类未override ToString()方法.那么调用Object.ToStrin ...
- VS2015使用技巧 为什么我们可以输入cw后按两下tab键出现console.writeline
镇场诗: 大梦谁觉,水月中建博客.百千磨难,才知世事无常. 今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 为什么 ...
- C# Winform里面用Console.WriteLine输出到哪了
C# Winform里面用Console.WriteLine输出也不会报错 显示在 VS IDE 的视图→输出窗口,且只在 Debug 环境下此语句执行. 如果是 Release 环境,在 Win32 ...
- C#核编之System.Console类
顾名思义,Console类封装了基于控制台的输入输出和错误流的操作,下面列举一些System.Console类常用的成员的,这些成员能为简单的命令行程序添加一些"情趣",例如改变背 ...
- 第一个输出程序 Console.WriteLine
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#中Console.WriteLine()函数输出格式详解
格式项都采用如下形式: {index[,alignment][:formatString]} 其中"index"指索引占位符,这个肯定都知道: ",alignment&q ...
- 看到Console.WriteLine($"string")写法,一时间不理解$的用途
参了网上资料,原来它是C# 6.0的语法糖. C# 6.0 新加上的功能: Null-Conditional Operator 大概就是,简洁代码量,缩短一些关于为null的判断~ 旧写法: pu ...
- c# System.Console
System.Console类公开了和操作控制台相关的有用的静态字段和静态方法.下面是System.Console中一些较为重要的方法. public static void Beep()该方法播放蜂 ...
- C#里面Console.Write()和Console.WriteLine()有什么区别?
Console.Write()和Console.WriteLine()都是System.Console提供的方法,两着主要用来将输出流由指定的输出装置(默认为屏幕)显示出来.两着间的差异在Consol ...
随机推荐
- 使用union来遍历结构体中的成员
前几天和实验室的同学讨论问题的时候发现他使用的一段数据校验的代码自己以前没有接触过,今天有空就把它整理了一下. #include <stdio.h> #include <stdlib ...
- display与position之间的关系
以防自己忘记写的 网上找的 positon 与 display 的相互关系 元素分为内联元素和区块元素两类(当然也有其它的),在内联元素中有个非常重要的常识,即内两元素是不可以设置区块元素所具有的样式 ...
- mysql数据库int(5)以及varchar(20)长度表示的是什么?
在mysql5.x版本的数据库中: int类型数据的字节大小是固定的4个字节: 但是int(5)和int(11)区别在于,显示的数据位数一个是5位一个是11位,在开启zerofill(填充零)情况下, ...
- ubuntu 下minicom超级终端的使用方法
http://blog.chinaunix.net/uid-25909619-id-3184639.html Ubuntu下使用sshfs挂载远程目录到本地 http://blog.csdn.net/ ...
- ADT-23.0.2百度网盘下载地址
最近 Google 被墙 http://download.csdn.net/download/wentai2009/7736389
- Android Market google play store帐号注册方法流程 及发布应用注意事项
Android Market google play store帐号申请 注册方法流程 在 Google Play 中发布软件之前,您需要完成以下三项工作: 创建开发人员个人资料 接受开发人员分发协议 ...
- C#对Execl操作类
1.NuGet下安装 NPOI 2.实例代码:(可以根据具体情况注释和添加代码逻辑) public class ExeclHelper { /// <summary> /// 将excel ...
- 给 asp.net core 写一个简单的健康检查
给 asp.net core 写一个简单的健康检查 Intro 健康检查可以帮助我们知道应用的当前状态是不是处于良好状态,现在无论是 docker 还是 k8s 还是现在大多数的服务注册发现大多都提供 ...
- [CentOS7] parted用于磁盘分区(同时支持GPT和MBR分区表)
声明:本文主要总结自:鸟哥的Linux私房菜-第七章.Linux 磁碟與檔案系統管理,如有侵权,请通知博主 fdisk支持MBR分区表,gdisk支持GPT分区表,而parted支持两者 不知道为什么 ...
- CF 979D Kuro and GCD and XOR and SUM(异或 Trie)
CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...