如何为Console.WriteLine创建快捷方式

我必须在我的代码中多次键入Console.WriteLine()所以任何人都可以告诉我为Console.WriteLine创建一个快捷方式,就像我可以使用它一样

CW=Console.WriteLine(); //After that i can use this CW for my Console.WriteLine() like CW("Print Something"); 

Visual Studio已经有一个默认的代码片段。 只需输入cw ,然后按Tab键。 请注意,如果您正在考虑使用某种方法,则可能缺少某些function,例如自动string.Format和其他重载参数。

如果您使用.net 3.5或更高版本:

 Action cw = Console.WriteLine; cw("Print Something"); 

毫无疑问,你可以为它创建一个Visual Studio片段 (虽然实际上有一个已经用于cw ,显然 – 试试吧!)。

我个人建议您不要 在代码中使用快捷方式 – 如果它仍然显示Console.WriteLine ,那么阅读它的人可能会更清楚。

根据它的用途,编写一个名为Log的辅助方法可能是有意义的 – 它具有合理的含义 ,而CW则没有。

(如果这用于日志记录,请考虑使用更强大的function,例如log4net 。)

C#6添加了using staticfunction:

 using static System.Console; class Program { void Main(string[] args) { WriteLine("Hello, {0}!", "world"); } } 

Visual Studio 2015中的IntelliSense了解这种新语法。

如果你想要它是全局的,你可以写一个扩展方法:

 public static class StringExtensions { public static void ConLog(this string msg) { Console.WriteLine(msg); } } 

现在无论你在哪里,你都可以调用"My Message".ConLog(); 在应用程序中的任何字符串上,并将其写入控制台。

 public static void CW(string str) { Console.WriteLine(str); } 

如果你有ReSharper,你可以输入和输入和行

 Console.Out.WriteLine(""); 

将写。

如果你想输出一个变量,还有另一个实时模板outv

 Console.Out.WriteLine("time = {0}", time); 

这里的时间是一个变量,您可以在输入outv后选择。

编写一个返回void的方法,并在Console.WriteLine()需要时调用

 void Log(string msg) { Console.WriteLine(msg); } 

您可以声明一个静态方法来包装调用:

 static class C { static void W(string s) { Console.WriteLine(s); } } 

然后:

 CW("Print Something"); 

在检入调用此方法的任何代码之前,我倾向于使用“内联方法”重构。 正如Jon Skeet所指出的那样,直接使用Console.WriteLine就不那么容易混淆了。

如果你在页面顶部写这个

using j = System.Console;

然后在任何时候你都可以使用

j.WriteLine("Anything you want to write");

这就是全部。 顺便说一句,你可以使用任何东西而不是“j”。

当您使用复合格式化重载(如Console.WriteLine(String, Object[])和格式化格式项的数量以及参数列表中的项目数args ,此快捷方式将避免抛出exception:

 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; } 
 // For formatting string and parameters define a function // The shortcut function wl, kind of write line public void wl( string format, params object[] parms){ Console.WriteLine(format, parms); } // Just for strings we can use Action delegate Action ws = Console.WriteLine; // examples: ws("String with no formatting parameters"); wl("String without formatting parameters"); wl("String with {0} parameters {1}", 2, "included"); wl("several parameters {0} {1} {2} repeated {0}", 1234, 5678, 6543); 

或者使用扩展方法:formatString.wl(arguments …)

 public static class ConsoleWriteExtensions { public static void wl(this string format, params object[] parms){ Console.WriteLine(format, parms); } } "{0} -> {1}".wl("Mili",123.45); // prints Mili -> 123.45 

我最喜欢的一个……来自BASIC和Python ……我经常错过Print()…我也经常在JS / ES中使用Print()来控制console.log / other-console …

所以将它声明为一个函数:

 public static void Print( object x ){ Console.WriteLine( x ); } 
  • 您可以随意组合字符串作为函数参数,如:
  Print( "Hi\n\n" + x.toString() + "\n\nBye!!!" ); 
  • 或随意插入变量
 Print( $"{x} ~ {y} ~ {z}" );