我在我的代码输入Console.WriteLine()多时间.. 所以谁能告诉我做一个快捷方式Console.WriteLine像我可以用它作为如何使一个快捷方式Console.WriteLine
CW=Console.WriteLine();
//After that i can use this CW for my Console.WriteLine() like
CW("Print Something");
我在我的代码输入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键。请注意,如果您正在考虑使用方法,它可能缺少一些功能,如自动string.Format和其他重载参数。
如果您在.NET 3.5或更高版本:
Action<string> cw = Console.WriteLine;
cw("Print Something");
编写一个返回void的方法和当过需要Console.WriteLine()
void Log(string msg)
{
Console.WriteLine(msg);
}
来电
public static void CW(string str)
{
Console.WriteLine(str);
}
你无疑可以为它创建一个Visual Studio snippet(尽管实际上已有一个用于cw
,显然 - 尝试它!)。
我个人建议你不使用代码内的快捷- 这可能是更清晰的任何人阅读它,如果它仍然说Console.WriteLine
。
根据这是什么的,它可以意义的写调用,比如一个辅助方法,Log
- 具有合理意思,而CW
没有。
(如果这是进行记录,可以考虑使用更强大的东西,如log4net了。)
你可以声明一个静态方法来包装电话:
static class C
{
static void W(string s)
{
Console.WriteLine(s);
}
}
则:
C.W("Print Something");
我会倾向于在检查任何代码之前使用“内联方法”重构s方法。正如Jon Skeet所指出的那样,直接使用Console.WriteLine并不那么容易混淆。
如果你想它全球性的,你可以写一个扩展方法:无论你在哪里,你可以在你的应用程序的任何字符串调用"My Message".ConLog();
并将其写入到控制台
public static class StringExtensions
{
public static void ConLog(this string msg)
{
Console.WriteLine(msg);
}
}
现在。
无法与Console.Writline的智能感 – 2013-11-25 12:01:45
如果你有ReSharper的您可以键入出和输入和行
Console.Out.WriteLine("");
将被写入。
如果你想输出变量还有另一个现场模板:OUTV。
Console.Out.WriteLine("time = {0}", time);
这里时间是你可以键入OUTV后选择一个变量。
如果你写这个页面
using j = System.Console;
的顶部,然后在任何时候都可以使用
j.WriteLine("Anything you want to write");
的这一切。顺便说一句,你可以使用任何东西而不是“j”。
此快捷方式将避免异常,当您使用复合格式化过载像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;
}
// 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<string> 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(参数...)
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
C# 6增加吨他using static
功能:
using static System.Console;
class Program {
void Main(string[] args) {
WriteLine("Hello, {0}!", "world");
}
}
Visual Studio 2015中的智能感知理解这种新的语法。
只需键入:cw ,然后按两次选项卡按钮。它实际上是用于编写Console.WriteLine()的visual studio代码片段。很快。就像你键入prop来声明类中的任何属性一样。
我的最爱之一也是... 来自BASIC和Python ...我错过了Print()往往... 我也在JS/ES中广泛使用Print()for console.log/other-控制台通常...
所以其声明为函数:
public static void Print(object x){ Console.WriteLine(x); }
Print("Hi\n\n" + x.toString() + "\n\nBye!!!");
Print($"{x} ~ {y} ~ {z}");
一起使用另外,使用快捷方式时,必须非常小心诸如参数列表中格式项和项之间不匹配的情况,如果您明确地调用了编译器,则编译器会提出警告'Console.WriteLine'。 我刚刚失去了一段时间,因为我的ReportTermination方法跟踪并写入控制台错误信息。我在方法中使用了一个常量格式的字符串(消息),因此我作为参数传递给格式字符串的属性被解释为格式字符串,将参数列表中的一个项目缩短,引发了另一个神秘异常。 – ProfK 2013-04-28 14:37:58
我的歉意。编译器不会生成警告;它警告说ReSharper。对带有不匹配参数列表的“Console.WriteLine”的任何标准调用都会引发异常。也许一个检查这个的捷径是值得的。 – ProfK 2013-04-28 14:54:17
值得赞赏! – 2013-11-25 12:00:22