2012-03-08 71 views
24

我在我的代碼輸入Console.WriteLine()多時間.. 所以誰能告訴我做一個快捷方式Console.WriteLine像我可以用它作爲如何使一個快捷方式Console.WriteLine

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

回答

87

Visual Studio已經有一個默認的代碼片段。只需輸入cw,然後按Tab鍵。請注意,如果您正在考慮使用方法,它可能缺少一些功能,如自動string.Format和其他重載參數。

+0

一起使用另外,使用快捷方式時,必須非常小心諸如參數列表中格式項和項之間不匹配的情況,如果您明確地調用了編譯器,則編譯器會提出警告'Console.WriteLine'。 我剛剛失去了一段時間,因爲我的ReportTermination方法跟蹤並寫入控制檯錯誤信息。我在方法中使用了一個常量格式的字符串(消息),因此我作爲參數傳遞給格式字符串的屬性被解釋爲格式字符串,將參數列表中的一個項目縮短,引發了另一個神祕異常。 – ProfK 2013-04-28 14:37:58

+0

我的歉意。編譯器不會生成警告;它警告說ReSharper。對帶有不匹配參數列表的「Console.WriteLine」的任何標準調用都會引發異常。也許一個檢查這個的捷徑是值得的。 – ProfK 2013-04-28 14:54:17

+0

值得讚賞! – 2013-11-25 12:00:22

30

如果您在.NET 3.5或更高版本:

Action<string> cw = Console.WriteLine; 

cw("Print Something"); 
+0

這是夢幻般的。是否可以添加一些描述到鼠標懸停時顯示的快捷方式? – Animesh 2012-07-19 05:37:08

+0

+1爲聰明,正是提問者想要的東西。但是,請注意會員訪問修改器:當開始沿着這條路走下去時,可能會出現不安全/不擇手段/不清楚的編程。 – jwrush 2012-08-23 19:11:09

+0

+1:在接受之後,我將成爲我的第二選擇答案。我喜歡這個,但它確實增加了風險。 – ProfK 2013-04-28 14:18:38

1

編寫一個返回void的方法和當過需要Console.WriteLine()

void Log(string msg) 
{ 
    Console.WriteLine(msg); 
} 
來電
2
public static void CW(string str) 
{ 
    Console.WriteLine(str); 
} 
12

你無疑可以爲它創建一個Visual Studio snippet(儘管實際上已有一個用於cw,顯然 - 嘗試它!)。

我個人建議你使用代碼內的快捷- 這可能是更清晰的任何人閱讀它,如果它仍然說Console.WriteLine

根據這是什麼的,它可以意義的寫調用,比如一個輔助方法,Log - 具有合理意思,而CW沒有。

(如果這進行記錄,可以考慮使用更強大的東西,如log4net了。)

1

你可以聲明一個靜態方法來包裝電話:

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

則:

C.W("Print Something"); 

我會傾向於在檢查任何代碼之前使用「內聯方法」重構s方法。正如Jon Skeet所指出的那樣,直接使用Console.WriteLine並不那麼容易混淆。

4

如果你想它全球性的,你可以寫一個擴展方法:無論你在哪裏,你可以在你的應用程序的任何字符串調用"My Message".ConLog();並將其寫入到控制檯

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

現在。

+0

無法與Console.Writline的智能感 – 2013-11-25 12:01:45

1

如果你有ReSharper的您可以鍵入輸入和行

Console.Out.WriteLine(""); 

將被寫入。

如果你想輸出變量還有另一個現場模板OUTV

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

這裏時間是你可以鍵入OUTV後選擇一個變量。

1

如果你寫這個頁面

using j = System.Console;

的頂部,然後在任何時候都可以使用

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

的這一切。順便說一句,你可以使用任何東西而不是「j」。

0

此快捷方式將避免異常,當您使用複合格式化過載像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; 
} 
0
// 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 
4

C# 6增加噸他using static功能:

using static System.Console; 

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

Visual Studio 2015中的智能感知理解這種新的語法。

0

只需鍵入:cw ,然後按兩次選項卡按鈕。它實際上是用於編寫Console.WriteLine()的visual studio代碼片段。很快。就像你鍵入prop來聲明類中的任何屬性一樣。

0

我的最愛之一也是... 來自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}");