为什么存在Console.Out.WriteLine? [英] Why does Console.Out.WriteLine exist?

查看:194
本文介绍了为什么存在Console.Out.WriteLine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,问题应该是为什么存在 Console.WriteLine 只是为了包装 Console.Out.WriteLine

Actually the question should be why does Console.WriteLine exist just to be a wrapper for Console.Out.WriteLine

我发现了这种使用intellisense的小方法,然后打开。NET反射器并反编译 Console.WriteLine 方法的代码,并发现以下内容:

I found this little method using intellisense, then opened .NET reflector and 'decompiled' the code for the Console.WriteLine method and found this:

public static void WriteLine(string value)
{
    Out.WriteLine(value);
}

为什么 WriteLine 以这种方式实施?

So why is WriteLine implemented this way? Is it totally just a shortcut or is there another reason?

推荐答案

Console.WriteLine 是静态方法。 Console.Out 是一个静态对象,可以将其作为参数传递给采用 TextWriter 的任何方法,并且方法可以调用非静态成员方法 WriteLine

Console.WriteLine is a static method. Console.Out is a static object that can get passed as a parameter to any method that takes a TextWriter, and that method could call the non-static member method WriteLine.

一个有用的示例是某种可自定义的日志记录例程,您可能希望将输出发送到 stdout Console.Out ), stderr Console.Error )或无处( System.IO.TextWriter.Null )或其他基于运行时条件的其他内容。

An example where this would be useful is some sort of customizable logging routines, where you might want to send the output to stdout (Console.Out), stderr (Console.Error) or nowhere (System.IO.TextWriter.Null), or anything else based on some runtime condition.

这篇关于为什么存在Console.Out.WriteLine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆