我在我的代碼輸入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