C# 如何为 Console.WriteLine 创建快捷方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9613685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a shortcut for Console.WriteLine
提问by Kishore Kumar
I have to type Console.WriteLine() many time in my code.. so can anyone tell me to make a shortcut for Console.WriteLine like i can use it as
我必须在我的代码中多次输入 Console.WriteLine() .. 所以有人可以告诉我为 Console.WriteLine 制作一个快捷方式,就像我可以使用它一样
CW=Console.WriteLine();
//After that i can use this CW for my Console.WriteLine() like
CW("Print Something");
采纳答案by Chibueze Opata
Visual Studio already has a default code snippet for this. Just type cw, and press tab. Note that if you're considering using a method, it may lack some features like the automatic string.Format and other overloaded parameters.
Visual Studio 已经为此提供了默认代码片段。只需键入cw,然后按 Tab。请注意,如果您正在考虑使用一种方法,它可能缺少一些功能,例如自动 string.Format 和其他重载参数。
回答by Michael Stum
If you are on .net 3.5 or newer:
如果您使用 .net 3.5 或更新版本:
Action<string> cw = Console.WriteLine;
cw("Print Something");
回答by Sai Kalyan Kumar Akshinthala
Write a method which returns void and call when ever required for Console.WriteLine()
编写一个方法,该方法返回 void 并在需要时调用 Console.WriteLine()
void Log(string msg)
{
Console.WriteLine(msg);
}
回答by Chris
public static void CW(string str)
{
Console.WriteLine(str);
}
回答by Jon Skeet
You could no doubt create a Visual Studio snippetfor it (although actually there's one already for cw, apparently - try it!).
毫无疑问,您可以为它创建一个Visual Studio 代码段(尽管实际上已经有一个用于cw,显然 - 试试吧!)。
I would personally suggest that you don'tuse a shortcut within the code- it's likely to be clearer to anyone readingit if it still says Console.WriteLine.
我个人建议你不要使用快捷方式的代码中-这可能是更清晰的任何人读它,如果它仍说Console.WriteLine。
Depending on what this is for, it maymake sense to write a helper method called, say, Log- that has a reasonable meaning, whereas CWdoesn't.
取决于这是为了什么,编写一个名为的辅助方法可能是有意义的,例如,Log- 具有合理的含义,而CW没有。
(If this isfor logging, consider using something more powerful such as log4net, too.)
(如果这是用于日志记录,也可以考虑使用更强大的东西,例如log4net。)
回答by phoog
You could declare a static method to wrap the call:
您可以声明一个静态方法来包装调用:
static class C
{
static void W(string s)
{
Console.WriteLine(s);
}
}
then:
然后:
C.W("Print Something");
I would be inclined to use the "inline method" refactoring before checking in any code that calls this method. As Jon Skeet notes, it's less confusing simply to use Console.WriteLine directly.
在签入调用此方法的任何代码之前,我倾向于使用“内联方法”重构。正如 Jon Skeet 所指出的,直接使用 Console.WriteLine 不会那么混乱。
回答by Sebastian P.R. Gingter
If you want it global, you could write a extension method:
如果你想要它是全局的,你可以编写一个扩展方法:
public static class StringExtensions
{
public static void ConLog(this string msg)
{
Console.WriteLine(msg);
}
}
Now wherever you are you can call "My Message".ConLog();on any string in your application and write it to the console.
现在无论您身在何处,都可以调用"My Message".ConLog();应用程序中的任何字符串并将其写入控制台。
回答by brgerner
If you have ReSharperyou can type outand Enterand the row
如果你有ReSharper的你可以键入出来,并进入与该行
Console.Out.WriteLine("");
will be written.
将被写入。
In case you want to output a variable there is another live template: outv.
如果您想输出一个变量,还有另一个实时模板:outv。
Console.Out.WriteLine("time = {0}", time);
Here timeis a variable which you could select after typing outv.
这里的time是一个变量,您可以在输入outv后选择它。
回答by Juanjo
If you write this at the top of the page
如果你在页面顶部写这个
using j = System.Console;
using j = System.Console;
then at any time you can use
然后你可以随时使用
j.WriteLine("Anything you want to write");
j.WriteLine("Anything you want to write");
An that's all. By the way, you can use anything instead of the "j".
仅此而已。顺便说一句,你可以使用任何东西来代替“j”。
回答by ProfK
This shortcut will avoid exceptions being thrown when you use a composite formatting overload like Console.WriteLine(String, Object[])and the number of format items in formatand the number of items in the argument list, args, differ:
当您使用复合格式重载时,此快捷方式将避免引发异常,例如Console.WriteLine(String, Object[]),格式项format数和参数列表中的项数args不同:
public bool WriteToConsole(string format, params object[] args)
{
var succeeded = false;
var argRegex = new Regex(@"\{\d+\}");
if ((args != null) && (argRegex.Matches(format).Count == args.Length))
{
Console.WriteLine(format, args);
succeeded = true;
}
else
{
Console.WriteLine(format);
}
return succeeded;
}

