Some Useful Visual Studio Code Snippets

September 25, 2018
Cover Image

Update: June 20, 2019 -- Here's an updated version of this post and the snippets including support for Visual Studio and Visual Studio for Mac.

A couple of months ago, I got inspired to really learn .NET Core and Visual Studio Code (VSCode).  But I didn't just want to learn it from Windows -- the real power of .NET Core is that's cross-platform.  So what did I do?  I bought a MacBook Pro and made that my primary work machine.

The thing that really became clear once I started working a lot in VSCode was how much I relied on the CodeRush templates in Visual Studio 2017 -- both the ones out of the box and the ones that I'd written -- to get work done.  I kept doing all the CodeRush keyboard shortcuts in VSCode that I had 'muscle memory' for and (of course) they did nothing.  (Grrr.)

Well, because I had a huge looming deadline for my next Pluralsight course, the only reasonable thing to do was to spend a day learning how to write VSCode Snippets and to write snippets for all the things that I missed from full Visual Studio 2017.

Here's my pile of C# snippets.  Download and enjoy.

To install these, open up Visual Studio Code, bring up the list of commands (CTRL-Shift-P / Command-Shift-P), and search for Configure User Snippets.  This will bring up your snippets info.  Choose 'csharp' and then copy the my snippets into your csharp.json snippets file.  You could also just replace your csharp.json snippets file with my file if you don't already have any snippets that you've written.

Once you've gotten those installed, to use them, type the keyboard shortcut value in VSCode and press Tab to expand the snippet.  Some of the snippets take multiple values.  Press Tab to move between the snippet values.

CategoryKeyboard ShortcutDescription
ASP.NETmcaASP.NET controller action method
ASP.NETmcapASP.NET controller HTTP post action method
ASP.NETr404ASP.NET controller return 404 not found
ASP.NETrbrASP.NET controller return bad request
ASP.NETrnfASP.NET controller return 404 not found
ASP.NETrrASP.NET controller redirect to action name
ASP.NETrridASP.NET controller redirect to action with id
CodingbCode block braces
CodingcPublic class
CodingcwlConsole.WriteLine
Codingfeforeach statement
CodingrReturn
CodingrfReturn false
CodingrseReturn String.Empty
CodingrtReturn true
Coding (if/else)enEquals null
Coding (if/else)eseEquals empty string
Coding (if/else)ifbif block
Coding (if/else)ifnIf variable is null
Coding (if/else)ifnaIf variable is null assign
Coding (if/else)inenIf variable is not null
Coding (if/else)lbElse block with braces
Coding (if/else)liElse if
Coding (if/else)neNot Equal
Coding (if/else)nenNot equal to null
Entity FrameworkaspdbcRegister DbContext
Entity FrameworkdbcDbContext class
Entity FrameworkdbcfCreate DbContextFactory
Entity FrameworkdbsDbSet<T>
ExceptionstcbTry catch block
ExceptionsianIf argument null throw
ExceptionsiansIf argument is empty string throw
ExceptionstaneThrow ArgumentNullException
ExceptionstnieThrow not implemented exception
ExceptionstnioeThrow invalid operation exception
ExceptionsuitrywinUser interface try catch
MethodsmiMethod that returns an int
MethodsmfMethod that returns an float
MethodsmsMethod that returns an string
MethodsmvMethod that returns an void
MethodsmmMethod that returns a type of your choice
MiscellaneousadpAdapt property
PropertiesapAuto-implemented property
PropertiesapbAuto-implemented bool property
PropertiesapfAuto-implemented float property
PropertiesapiAuto-implemented int property
PropertiesapsAuto-implemented string property
PropertiespProperty
PropertiespsProperty (string)
PropertiesrbRead-only bool property
PropertiesriRead-only int property
PropertiesrpRead-only property
PropertiesrsRead-only string property
StringsseString.Empty
StringsstfString.Format
StringsstpString.Format with paste
StringsnsbCreate a new StringBuilder
Unit TestingaeAssert AreEqual with T
Unit Testingae0Assert.AreEqual to 0
Unit TestingaepAssert.AreEqual on two properties
Unit TestingaepbAssert.AreEqual on two boolean properties
Unit Testingaepd8Assert.AreEqual on two DateTime properties
Unit TestingaepfAssert.AreEqual on two float properties
Unit TestingaepiAssert.AreEqual on two int properties
Unit TestingaepsAssert.AreEqual on two string properties
Unit TestingafAssert.IsFalse
Unit TestingafaAssert.Fail()
Unit TestingainAssert.Inconclusive()
Unit TestingainesAssert string is null or empty
Unit TestinganAssert is null
Unit TestinganeAssert is not equal with choice of T
Unit Testingane0Assert is not equal to zero
Unit TestinganefAssert floats are not equal
Unit TestinganeiAssert ints are not equal
Unit TestinganepbAssert bool properties are not equal
Unit TestinganepiAssert int properties are not equal
Unit TestinganepsAssert string properties are not equal
Unit TestinganesAssert strings are not equal
Unit TestinganeseAssert string is not empty string
Unit TestingannAssert is not null
Unit TestingannsAssert is not null or empty string
Unit TestingansAssert is null or empty string
Unit TestingaseAssert.AreEqual to String.Empty
Unit TestingatAssert is true
Unit TestingmvtTest Method
Unit TestingotiOn Test Initialize
Unit TestingsutSystemUnderTest property for unit tests
Unit TestingsutnSet SystemUnderTest variable to null
Unit TestingtcCreate test class
Unit TestingtfCreate test class
Unit TestingtfsutCreate test class with sut

Once again, here's the download link for the csharp snippets.

I hope you get some use out of these.

-Ben