2017-07-27 89 views
1

我經常編寫一個Console.WriteLine語句,然後在稍後的階段進行修改。然後我最終得到這樣的陳述。命名參數Console.WriteLine c#

Console.WriteLine("{2} is not greater than {0} or smaller than {1}",min,max, grade); 

有沒有辦法明確命名傳遞給Console.WriteLine命令的參數,所以,我並不需要通過最小,最大和檔次的順序?

作爲一個相關說明,我經常刪除要在Console.WriteLine中打印的變量,例如下面的{2}已被刪除。

Console.WriteLine("{3} is not greater than {0} or smaller than {1}",min,max, grade); 

有沒有辦法不更改編號,說是這樣的:以類似的方式

Console.WriteLine("{3} is not greater than {0} or smaller than {1}",{0}:min,{1}:max, {3}: grade); 

像這樣

@Html.ActionLink("Your profile", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" }); 
+0

使用C#6字符串插值:'$「{grade}不會更大......」'。 – CodeCaster

+0

爲什麼不使用字符串插值? $「{grade}不大於{min}或小於{max}」 – Stuart

+0

請注意,字符串中沒有自動完成。 –

回答

3

取而代之的是聲明:

Console.WriteLine("{3} is not greater than {0} or smaller than {1}",min,max, grade); 

你也可以使用C #6字符串插入語法:

Console.WriteLine($"{grade} is not greater than {min} or smaller than {max}"); 
0

是的。

現在,您可以把字符串這樣

Console.WriteLine($"{grade} is not greater than {max} or smaller than {min}"); 

注意$符號在前面的字符串。