I find myself adding dependencies a lot to constructors like so:
public class SomeClass() {
private ISomeService _service;
private IAnotherService _anotherService;
public SomeClass(ISomeService service, IAnotherService anotherService) {
_service = service;
_anotherService = anotherService;
}
}
They're quite tedious to write, I've been looking for code snippets in visual studio to automatically add one to the constructor but haven't found one.
What I want is:
OR
For a dependency property, use propdp. You can find out all available snippets easily if you're using visual studio 2017: Type a letter, for example a. Click on the last icon at the bottom of the Intellisense Window. This displays all available snippets.
In Visual Studio Code, snippets appear in IntelliSense (Ctrl+Space) mixed with other suggestions, as well as in a dedicated snippet picker (Insert Snippet in the Command Palette). There is also support for tab-completion: Enable it with "editor.tabCompletion": "on", type a snippet prefix (trigger text), and press Tab to insert a snippet.
For the full list of snippets (little bits of prefabricated code) press Ctrl+K and then Ctrl+X. Source from MSDN. Works in Visual Studio 2013 with a C# project. So how to make a constructor. Press Ctrl+K and then Ctrl+X. Select Visual C#.
When adding a dependency to a constructor some snippet automatically creates a local variable and assigns to it. Add a private variable and then some snippet automatically adds it to the constructor and assigns it to the local variable. Show activity on this post.
If you have R# you can enter the field declarations and then highlight them and hit Alt-Enter
which will give you the option to generate the constructor and field assignments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With