TL;DR -- Here are the links to download the code snippets:
- Snippets for Visual Studio 2019
- Snippets for Visual Studio for Mac
- Snippets for Visual Studio Code
- Download the snippets from GitHub
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
Category | Shortcut | Description |
ASP.NET | mca | ASP.NET controller action method |
ASP.NET | mcap | ASP.NET controller HTTP post action method |
ASP.NET | r404 | ASP.NET controller return 404 not found |
ASP.NET | rbr | ASP.NET controller return 404 not found |
ASP.NET | rnf | ASP.NET controller return 404 not found |
ASP.NET | rr | ASP.NET controller redirect to action name |
ASP.NET | rrid | ASP.NET controller redirect to action with id |
Coding | b | Code block braces |
Coding | c | Public class |
Coding | cwl | Console.WriteLine |
Coding | fe | foreach statement |
Coding | i | Declare a C# interface |
Coding | mb | Declare a method that returns a bool |
Coding | r | Return |
Coding | rf | Return false |
Coding | rse | Return String.Empty |
Coding | rt | Return true |
Coding (if/else) | en | Equals null |
Coding (if/else) | ese | Equals empty string |
Coding (if/else) | ifb | if block |
Coding (if/else) | ifn | If variable is null |
Coding (if/else) | ifna | If variable is null assign |
Coding (if/else) | inen | If variable is not null |
Coding (if/else) | lb | Else block with braces |
Coding (if/else) | li | Else if |
Coding (if/else) | ne | Not Equal |
Coding (if/else) | nen | Not equal to null |
Entity Framework | aspdbc | Register DbContext |
Entity Framework | dbc | DbContext class |
Entity Framework | dbcf | Create DbContextFactory |
Entity Framework | dbs | DbSet<T> |
Exceptions | ian | If argument null throw |
Exceptions | ians | If argument is empty string throw |
Exceptions | tane | Throw ArgumentNullException |
Exceptions | tcb | Try catch block |
Exceptions | tnie | Throw not implemented exception |
Exceptions | tnioe | Throw invalid operation exception |
Exceptions | uitrywin | User interface try catch |
Methods | mf | Method that returns an float |
Methods | mi | Method that returns an int |
Methods | mm | Method that returns a type of your choice |
Methods | ms | Method that returns an string |
Methods | mv | Method that returns an void |
Miscellaneous | adp | Adapt property |
Properties | ap | Auto-implemented property |
Properties | apb | Auto-implemented bool property |
Properties | apf | Auto-implemented float property |
Properties | api | Auto-implemented int property |
Properties | aps | Auto-implemented string property |
Properties | p | Property |
Properties | ps | Property (string) |
Properties | rb | Read-only bool property |
Properties | ri | Read-only int property |
Properties | rp | Read-only property |
Properties | rs | Read-only string property |
Strings | nsb | Create a new StringBuilder |
Strings | se | String.Empty |
Strings | stf | String.Format |
Strings | stp | String.Format with paste |
Unit Testing | aaa | Add Arrange Act Assert comments |
Unit Testing | ae | Assert AreEqual with T |
Unit Testing | ae0 | Assert.AreEqual to 0 |
Unit Testing | aep | Assert.AreEqual on two properties |
Unit Testing | aepb | Assert.AreEqual on two boolean properties |
Unit Testing | aepd8 | Assert.AreEqual on two DateTime properties |
Unit Testing | aepf | Assert.AreEqual on two float properties |
Unit Testing | aepi | Assert.AreEqual on two int properties |
Unit Testing | aeps | Assert.AreEqual on two string properties |
Unit Testing | aes | Assert.AreEqual on two strings |
Unit Testing | af | Assert.IsFalse |
Unit Testing | afa | Assert.Fail() |
Unit Testing | ain | Assert.Inconclusive() |
Unit Testing | aines | Assert string is null or empty |
Unit Testing | an | Assert is null |
Unit Testing | ane | Assert is not equal with choice of T |
Unit Testing | ane0 | Assert is not equal to zero |
Unit Testing | anef | Assert floats are not equal |
Unit Testing | anei | Assert ints are not equal |
Unit Testing | anepb | Assert bool properties are not equal |
Unit Testing | anepi | Assert int properties are not equal |
Unit Testing | aneps | Assert string properties are not equal |
Unit Testing | anes | Assert strings are not equal |
Unit Testing | anese | Assert string is not empty string |
Unit Testing | ann | Assert is not null |
Unit Testing | anns | Assert is not null or empty string |
Unit Testing | ans | Assert is null or empty string |
Unit Testing | ase | Assert.AreEqual to String.Empty |
Unit Testing | at | Assert is true |
Unit Testing | mvt | Test Method |
Unit Testing | oti | On Test Initialize method for MSTest |
Unit Testing | otisutn | On Test Initialize method that sets system under test to null |
Unit Testing | sut | SystemUnderTest property for unit tests |
Unit Testing | sutn | Set SystemUnderTest variable to null |
Unit Testing | tc | Create test class |
Unit Testing | tf | Create test class |
Unit Testing | tfsut | Create 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