Industry Insights, Information, and Developer News
If you're like me, you probably write a lot of the same boilerplate C# code over and over again. Maybe it's for test setup, property declarations, or spinning up a new DbContext. And while I love IntelliSense, sometimes I just want to type a few characters and have a whole code block explode into place.
That's where Visual Studio Code's user-defined snippets come in. They're like supercharged autocomplete -- and once you get the hang of them, you'll wonder how you ever lived without them.
In this post, I'll show you how to create your own C# snippets, highlight a few of my favorites, and show you some tips for getting the most out of them in VS Code.
What's a VS Code Snippet? Snippets are reusable code templates that you can trigger by typing a prefix. You type the prefix, hit Tab, and boom -- the snippet expands into a fully-formed block of code with placeholders that you can tab through and customize.
They live in a JSON file. For C#, that file is csharp.json.
To get there:
A Simple Snippet Here's a basic snippet that expands into a Console.WriteLine() with an interpolated string.
"log": { "prefix": "log", "body": [ "Console.WriteLine($\"${1:message} = {${1}}${0}\");" ], "description": "Console.WriteLine with interpolated value" }
Type log in your VSCode editor window and hit Tab. It'll drop in:
Console.WriteLine($"message = {message}");
You can tab through and change the message placeholder.
My Favorite Snippets Here are a few of the ones I've built that I use constantly when I'm working in VS Code:
Lazy System Under Test ('sut')
This is great for test classes where you're instantiating the class you're testing.
"sut": { "prefix": "sut", "body": [ "private ${1:MyClass}? _${2:myClass};", "", "private ${1:MyClass} GetSystemUnderTest()", "{", " _${2:myClass} ??= new ${1:MyClass}();", " return _${2:myClass};", "}" ], "description": "Lazy-initialized System Under Test" }
Arrange / Act / Assert Block ('aaa')
Just a comment block -- but it helps enforce test structure.
"aaa": { "prefix": "aaa", "body": [ "// arrange", "", "// act", "", "// assert" ], "description": "Arrange / Act / Assert test structure" }
xUnit Constructor with ITestOutputHelper ('ctortest')
If you write a lot of unit tests with xUnit, you know that you can't just write messages to your test output using Console.WriteLine() -- instead, you've got to go through xUnit's ITestOutputHelper interface. In order to make this Console.WriteLine() thing and some other testing things (for example, working with test data files) easy, I've got a TestClassBase in my Benday.Common.Testing library and NuGet package.
This snippet creates a constructor that hooks all that up and passes the right values into the base class. One of the (I think) really cool bits of this snippet is the use of ${TM_FILENAME_BASE} to generate the method name for the constructor. As long as your filename matches the class name, this will create exactly the constructor code that you need.
"ctortest": { "prefix": "ctortest", "body": [ "public ${TM_FILENAME_BASE}(ITestOutputHelper output) : base(output)", "{", "}" ], "description": "xUnit constructor with output" }
DbContext Factory for EF Core ('dbcf')
Here's a case where it's just a whole lot of boilerplate code that I need to add to a lot of different projects. When you're working with Entity Framework Migrations, managing configurations gets a lot easier if you add an implementation of IDesignTimeDbContextFactory to your projects. With this snippet, I can just drop in my DbContextFactory implementation with just a little bit of typing.
"dbcf": { "prefix": "dbcf", "body": [ "public class ${1:MyApp}DbContextFactory : IDesignTimeDbContextFactory<${1:MyApp}DbContext>", "{", " public ${1:MyApp}DbContext CreateDbContext(string[] args)", " {", " var environment = Environment.GetEnvironmentVariable(\"ASPNETCORE_ENVIRONMENT\") ?? \"Development\";", " var builder = new ConfigurationBuilder()", " .SetBasePath(Directory.GetCurrentDirectory())", " .AddJsonFile(\"appsettings.json\", optional: false, reloadOnChange: true)", " .AddJsonFile($\"appsettings.{environment}.json\", optional: true)", " .AddUserSecrets<${1:MyApp}DbContextFactory>()", " .AddEnvironmentVariables();", "", " var config = builder.Build();", "", " var optionsBuilder = new DbContextOptionsBuilder<${1:MyApp}DbContext>();", " var connectionString = config.GetConnectionString(\"Default\");", "", " optionsBuilder.UseSqlServer(connectionString);", "", " return new ${1:MyApp}DbContext(optionsBuilder.Options);", " }", "}" ], "description": "Design-time DbContext factory for EF Core" }
Tips for Smarter Snippets Here are a few tricks I've picked up along the way:
Summary Snippets are one of those things that seem like a novelty at first -- but once you use them, they become essential. If you're working in VS Code and writing C#, give them a try. Start small, build up your own snippet library, and save yourself from typing the same boilerplate a million times.
I've posted my full set of snippets here on GitHub. Feel free to grab them or fork and tweak for your own style.
About the Author
Posted by Benjamin Day on 07/22/2025