Code Snippets for Visual Studio, Visual Studio Code, and Visual Studio for Mac

June 20, 2019
Cover Image

TL;DR -- Here are the links to download the code snippets:

Some Background

My coding world and coding life has changed a lot in the last year and a lot of it was driven by moving to a Macbook Pro. I moved to a mac not so much because I love MacOS -- it's mostly about forcing myself to learn to code .NET Core and ASP.NET Core without relying on Visual Studio. That means that I'm using Visual Studio Code for most of my work.

That initial transition from Visual Studio to Visual Studio Code was a little rough and a lot of it was because I not only leaned heavily on Visual Studio but I also leaned super hard on CodeRush and my custom CodeRush templates that I'd created over the years. It was painful enough that I rewrote all my CodeRush templates as Visual Studio Code snippets.

Our Hero Tries New Things and Doesn't Hate Them

Fast-forward about 9 months and I've been switching between Mac and Windows a lot. Then a couple of things happened. 1) I had a conversation with Joseph Hill about Visual Studio for Mac and 2) I saw this Visual Studio Productivity Tips video by Robert Green and Kendra Havens.

Joseph drove home the point that Visual Studio for Mac is *NOT* just for developing Xamarin-based apps -- it's also good for doing ASP.NET Core development. He suggested that I go give Visual Studio for Mac another try. I did...and it's good.

Then there's the Robert & Kendra video. That video is mind-blowing. I've been using Visual Studio for *DECADES* and there were a ton of features that I had no idea about. I decided to give Visual Studio a try without CodeRush...and it's good. There are still some things that I miss from CodeRush but whateryagunnado? (shrug)

Code Snippets for Mac and Code Snippets for Visual Studio

So I started using Visual Studio for Mac a lot and I started using Visual Studio 2019 a lot without running CodeRush. But I still needed/wanted the snippets that I wrote for Visual Studio Code.

One thing that's a little frustrating is that Visual Studio Code, Visual Studio for Mac, and Visual Studio all have different formats for their snippets. So I wrote a utility to read all my Visual Studio Code snippets and translate them to Visual Studio snippet format and Visual Studio for Mac snippet format.

The Snippets

CategoryShortcutDescription
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 404 not found
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
CodingiDeclare a C# interface
CodingmbDeclare a method that returns a bool
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>
ExceptionsianIf argument null throw
ExceptionsiansIf argument is empty string throw
ExceptionstaneThrow ArgumentNullException
ExceptionstcbTry catch block
ExceptionstnieThrow not implemented exception
ExceptionstnioeThrow invalid operation exception
ExceptionsuitrywinUser interface try catch
MethodsmfMethod that returns an float
MethodsmiMethod that returns an int
MethodsmmMethod that returns a type of your choice
MethodsmsMethod that returns an string
MethodsmvMethod that returns an void
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
StringsnsbCreate a new StringBuilder
StringsseString.Empty
StringsstfString.Format
StringsstpString.Format with paste
Unit TestingaaaAdd Arrange Act Assert comments
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 TestingaesAssert.AreEqual on two strings
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 method for MSTest
Unit TestingotisutnOn Test Initialize method that sets system under test to null
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 system under test property

How to Install the Snippets for Visual Studio Code

C# Code Snippets for Visual Studio Code

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.

How to Install the Snippets for Visual Studio 2019

C# Code Snippets for Visual Studio 2019

To install these snippets for Visual Studio 2019, first make sure that Visual Studio is not running. Then you'll need to copy the snippets to the snippets folder using Explorer.exe. The folder name is %userprofile%\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets. If you've never customized a snippet in Visual Studio before, this directory might not exist. Just create it and copy the snippets to that directory.

How to Install the Snippets for Visual Studio for Mac

C# Code Snippets for Visual Studio for Mac

To install these snippets for Visual Studio for Mac, first make sure that Visual Studio for Mac is not running. Then you'll need to copy them to the snippet folder using Finder. The folder name is ~/Library/VisualStudio/8.0/Snippets. If you've never customized a snippet in Visual Studio for Mac before, this directory might not exist. Just create it and copy the snippets to that directory.

Summary

Anyway these snippets make my life as a developer a lot easier. Hopefully, you'll get some use out of them. If you want to download them from GitHub or want to contribute, here's the GitHub repository.

Hope this helps.

-Ben

Tags: snippets visual-studio visual-studio-code-vscode visual-studio-for-mac