How can I type “{0}” more quickly in Visual Studio?

痴心易碎 提交于 2019-12-13 09:44:35

问题


In C#, it's common to have to type {0}, {1}, etc. when formatting strings with string.Format() or Console.WriteLine(). Considering its frequency, it's an awkward set of key strokes. But despite my searches, I couldn't find any sort of shorthand or hotkey to automatically insert it in Visual Studio. Has anyone figured out something to expedite the process?


回答1:


You can create the following command (C#) with my Visual Commander extension to insert text and then assign a keyboard shortcut to it:

    EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
    ts.Text = "{0}";



回答2:


One of the benefits from string interpolation in C#6.0, which I find helps.

Instead of:

string s = String.Format("{0} and {1}", variable1, variable2);

You can do:

string s = $"{variable1} and {variable2}";

See a guide: http://www.informit.com/articles/article.aspx?p=2422807



来源:https://stackoverflow.com/questions/47241777/how-can-i-type-0-more-quickly-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!