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?
Asked
Active
Viewed 172 times
-3

Kyle Delaney
- 11,616
- 6
- 39
- 66
-
6use autohotkey. – Johan Lundberg Nov 11 '17 at 19:09
-
1[Macros](https://msdn.microsoft.com/en-us/library/b4c73967(v=vs.100).aspx) might be able to do what you want. – hnefatl Nov 11 '17 at 19:10
-
2Using C# 6 you can simply place a $ before the string and use our variables directly like this: `string name; string msg = $"Hello {name}"; ` This is also better readable than `{0}`. – Lion Nov 11 '17 at 19:10
-
1@Lion Indeed interpolation is ideal for non-localizable text (guide - https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/string-interpolation), if code must support multiple languages than interpolated strings are much less useful as currently there is no support for localization - https://stackoverflow.com/questions/29068194/c6-0-string-interpolation-localization... Which may be the reason OP still using `{0}`. – Alexei Levenkov Nov 11 '17 at 19:33
-
You are my idol and hero if that seems to be the issue and obstacle you are facing as a programmer ;) – CodingYoshi Nov 11 '17 at 19:38
-
@hnefatl, are macros still around or have they been deprecated? – Kyle Delaney Nov 11 '17 at 19:41
-
1I'm almost motivated to go and write a VS extension for this :) – Ben Hall Nov 11 '17 at 20:47
-
1@KyleDelaney Good spot, I didn't notice that. There's an extension [here](https://marketplace.visualstudio.com/items?itemName=XavierPoinas.TextMacrosforVisualStudio201220132015) that seems to provide a good drop-in replacement, though. – hnefatl Nov 11 '17 at 22:39
-
Why the downvotes? I did my research, I included hyperlinks, I was clear and concise, and it's a question that's likely to help other developers... – Kyle Delaney Nov 13 '17 at 14:14
2 Answers
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

Ben Hall
- 1,353
- 10
- 19
-
That's good to know! But it doesn't address my question. The $ syntax may be easier to read but it's just as difficult to type because you're still using curly braces. I'm looking for some kind of shortcut or autocomplete. – Kyle Delaney Nov 11 '17 at 19:34
-
Thats not why MS came up with interpolation. Plus it still takes the same amount of keystrokes. – CodingYoshi Nov 11 '17 at 19:35
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}";

Sergey Vlasov
- 26,641
- 3
- 64
- 66