.NET Snippets for Visual Studio Code⚠️ Disclaimer ⚠️This is not the official Visual Studio Code or Microsoft extension. This is a community-created extension that provides C#, ASP.NET Core, and Razor/Blazor snippets to enhance your development workflow. A comprehensive collection of code snippets for .NET development in Visual Studio Code, covering C#, ASP.NET Core, and Razor/Blazor development. FeaturesC# Snippets (
|
Prefix | Description | Output | Tab stops |
---|---|---|---|
for | C# for loop | for (int i = 0; i < length; i++) { ... } | $1 (i), $2 (length), $0 (final cursor) |
foreach | C# foreach loop | foreach (var item in collection) { ... } | $1 (item), $2 (collection), $0 (final cursor) |
if | If statement | if (condition) { ... } | $1 (condition), $0 (final cursor) |
try | Try-catch block | try { ... } catch (Exception ex) { ... } | $0 (final cursor), $1 (Exception) |
main | Main method for console app | public static void Main(string[] args) { ... } | $0 (final cursor) |
class | Basic class definition | public class ClassName { ... } | $1 (ClassName), $0 (final cursor) |
prop | Auto-implemented property | public string PropertyName { get; set; } | $1 (string), $2 (PropertyName) |
ctor | Constructor for a class | public ClassName() { ... } | $1 (ClassName), $0 (final cursor) |
async | Async method definition | public async Task MethodNameAsync() { ... } | $1 (MethodName), $0 (final cursor) |
linq | LINQ query expression | var result = from item in collection where item.Property == value select item; | $1 (item), $2 (collection), $3 (Property), $4 (value) |
controller | ASP.NET Core API controller with logging | [ApiController] [Route("api/[controller]")] public class ControllerNameController : ControllerBase { ... } | $1 (Namespace), $2 (ControllerName), $0 (final cursor) |
get | HTTP GET endpoint with error handling | [HttpGet("route")] public async Task<ActionResult> GetName(parameters) { ... } | $1 (route), $2 (ReturnType), $3 (Name), $4 (parameters), $0 (final cursor) |
post | HTTP POST endpoint with error handling | [HttpPost("route")] public async Task<ActionResult> PostName([FromBody] ModelType model) { ... } | $1 (route), $2 (ReturnType), $3 (Name), $4 (ModelType), $0 (final cursor) |
inject | Constructor-based dependency injection | private readonly IServiceType _serviceName; public ClassName(IServiceType serviceName) { ... } | $1 (IServiceType), $2 (serviceName), $3 (ClassName) |
dbcontext | Entity Framework DbContext class | public class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) {} public DbSet Entities { get; set; } } | $1 (AppDbContext), $2 (Entity) |
dbset | EF Core DbSet property | public DbSet Entities { get; set; } | $1 (Entity) |
page | Razor page route directive | @page "/route" | $1 (route) |
code | Code block in Razor | @code { ... } | $0 (final cursor) |
rfor | foreach loop in Razor | @foreach (var item in collection) { @item } |
$1 (item), $2 (collection), $0 (final cursor) |
razorinject | Inject service in Razor | @inject IMyService MyService | $1 (IMyService), $2 (MyService) |
form | Blazor EditForm with validation | ... | $1 (model), $2 (HandleValidSubmit), $3 (Property), $0 (final cursor) |
logger | Inject ILogger for logging | private readonly ILogger _logger; public ClassName(ILogger logger) { ... } | $1 (ClassName) |
route | Route mapping for controller/action | [Route("api/[controller]")] [ApiController] | $1 (api/[controller]) |
returnok | Returns Ok response | return Ok(new { property = value }); | $1 (property), $2 (value) |
middleware | Custom middleware with logging | public class MiddlewareNameMiddleware { ... } | $1 (MiddlewareName), $0 (final cursor) |
service | Create a new service interface and implementation | public interface IServiceNameService { ... } public class ServiceNameService : IServiceNameService { ... } | $1 (ServiceName), $0 (final cursor) |
C# Snippets (csharp.json)
Prefix | Description | Output | Tab stops |
---|---|---|---|
main | Entry point for a console app | public static void Main(string[] args) { ... } | $0 (final cursor) |
class | Basic class definition | public class ClassName { ... } | $1 (ClassName), $0 (final cursor) |
ctor | Constructor for a class | public ClassName(parameters) { ... } | $1 (ClassName), $2 (parameters), $0 (final cursor) |
prop | Auto-implemented property | public type PropertyName { get; set; } | $1 (type), $2 (PropertyName) |
for | Standard for loop | for (int i = 0; i < count; i++) { ... } | $1 (i), $2 (count), $0 (final cursor) |
foreach | Iterates through a collection | foreach (var item in collection) { ... } | $1 (item), $2 (collection), $0 (final cursor) |
if | Basic conditional | if (condition) { ... } | $1 (condition), $0 (final cursor) |
switch | Switch-case logic | switch (expression) { case value: ... break; default: break; } | $1 (expression), $2 (value), $0 (final cursor) |
try | Error handling with logging | try { ... } catch (Exception ex) { ... } | $1 (try block), $2 (Exception), $0 (final cursor) |
async | Async method with Task return | public async Task MethodNameAsync(parameters) { ... } | $1 (returnType), $2 (MethodName), $3 (parameters), $0 (final cursor) |
linq | LINQ query expression | var result = from item in collection where condition select item; | $1 (item), $2 (collection), $3 (condition) |
dbcontext | Entity Framework DbContext class | public class DbContextName : DbContext { ... } | $1 (DbContextName), $0 (final cursor) |
dbset | EF Core DbSet property | public DbSet EntityNames { get; set; } | $1 (EntityType), $2 (EntityName) |
testclass | xUnit/NUnit test class | public class TestClassNameTests { ... } | $1 (TestClassName), $2 (TestedClass), $3 (sut), $0 (final cursor) |
testmethod | Unit test method with AAA pattern | [Fact] public async Task MethodName_Scenario_ExpectedResult() { ... } | $1 (MethodName), $2 (Scenario), $3 (ExpectedResult), $4 (arrange), $5 (sut), $6 (parameters), $0 (final cursor) |
asserteq | Assert.Equal() usage | Assert.Equal(expected, actual); | $1 (expected), $2 (actual) |
logger | Inject and use ILogger | private readonly ILogger _logger; public ClassName(ILogger logger) { ... } | $1 (ClassName) |
config | Access config from IConfiguration | private readonly IConfiguration _configuration; public ClassName(IConfiguration configuration) { ... } var value = _configuration["Section:Key"]; | $1 (ClassName), $2 (Section:Key) |
nullcheck | Safe null check template | if (variable == null) { throw new ArgumentNullException(nameof(variable)); } | $1 (variable) |
Razor/Blazor Snippets (razor.json)
Prefix | Description | Output | Tab stops |
---|---|---|---|
for | C# for loop | for (int i = 0; i < length; i++) { ... } | $1 (i), $2 (length), $0 (final cursor) |
foreach | C# foreach loop | foreach (var item in collection) { ... } | $1 (item), $2 (collection), $0 (final cursor) |
if | If statement | if (condition) { ... } | $1 (condition), $0 (final cursor) |
try | Try-catch block | try { ... } catch (Exception ex) { ... } | $0 (final cursor), $1 (Exception) |
main | Main method for console app | public static void Main(string[] args) { ... } | $0 (final cursor) |
class | Basic class definition | public class ClassName { ... } | $1 (ClassName), $0 (final cursor) |
prop | Auto-implemented property | public string PropertyName { get; set; } | $1 (string), $2 (PropertyName) |
ctor | Constructor for a class | public ClassName() { ... } | $1 (ClassName), $0 (final cursor) |
async | Async method definition | public async Task MethodNameAsync() { ... } | $1 (MethodName), $0 (final cursor) |
linq | LINQ query expression | var result = from item in collection where item.Property == value select item; | $1 (item), $2 (collection), $3 (Property), $4 (value) |
controller | ASP.NET Core API controller | [ApiController] [Route("api/[controller]")] public class MyController : ControllerBase { ... } | $1 (MyController), $0 (final cursor) |
get | HTTP GET endpoint | [HttpGet] public IActionResult GetSomething() { return Ok(data); } | $1 (GetSomething), $2 (data) |
post | HTTP POST endpoint | [HttpPost] public IActionResult PostSomething([FromBody] Model model) { return CreatedAtAction(nameof(GetSomething), new { id = model.Id }, model); } | $1 (PostSomething), $2 (Model), $3 (GetSomething) |
inject | Constructor-based dependency injection | private readonly IMyService _service; public ClassName(IMyService service) { ... } | $1 (IMyService), $2 (service), $3 (ClassName) |
dbcontext | Entity Framework DbContext class | public class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) {} public DbSet Entities { get; set; } } | $1 (AppDbContext), $2 (Entity) |
dbset | EF Core DbSet property | public DbSet Entities { get; set; } | $1 (Entity) |
page | Razor page route directive | @page "/route" | $1 (route) |
code | Code block in Razor | @code { ... } | $0 (final cursor) |
rfor | foreach loop in Razor | @foreach (var item in collection) { @item } |
$1 (item), $2 (collection), $0 (final cursor) |
razorinject | Inject service in Razor | @inject IMyService MyService | $1 (IMyService), $2 (MyService) |
form | Blazor EditForm with validation | ... | $1 (model), $2 (HandleValidSubmit), $3 (Property), $0 (final cursor) |
logger | Inject ILogger for logging | private readonly ILogger _logger; public ClassName(ILogger logger) { ... } | $1 (ClassName) |
page (Razor Page) | Razor page with model and title | @page @model Namespace.PageModel @{ ViewData["Title"] = "Page Title"; } @ViewData["Title"] ... |
$1 (Namespace), $2 (PageModel), $3 (Page Title), $0 (final cursor) |
code (Blazor code block) | Blazor code block with initialization | @code { private type variable; protected override async Task OnInitializedAsync() { ... } } | $1 (type), $2 (variable), $0 (final cursor) |
rfor (Razor Loop) | Razor foreach loop with HTML structure | @foreach (var item in collection) { @item.Property } |
$1 (item), $2 (collection), $3 (class), $4 (Property) |
component | Blazor component with parameter and initialization | @page "/route" ComponentName ... @code { [Parameter] public type ParameterName { get; set; } protected override async Task OnInitializedAsync() { ... } } |
$1 (route), $2 (ComponentName), $3 (class), $4 (type), $5 (ParameterName), $0 (final cursor) |
form (Blazor Form) | Blazor form with validation and styling | ... @code { private ModelType model = new(); private async Task HandleValidSubmit() { ... } } | $1 (model), $2 (HandleValidSubmit), $3 (Property), $4 (Label), $5 (ModelType), $0 (final cursor) |
bind | Blazor input with validation | <InputText @bind-Value="@model.Property" class="form-control" /> | $1 (model), $2 (Property) |
razorinject (Inject Service) | Inject service in Blazor component | @inject IServiceType ServiceName @code { // Use ServiceName in your component ... } | $1 (IServiceType), $2 (ServiceName), $0 (final cursor) |
razorlayout | Razor layout with Bootstrap and scripts | ... @RenderBody() ... |
$1 (ApplicationName), $2 (header), $3 (footer) |
section | Define a Razor section | @section SectionName { ... } | $1 (SectionName), $0 (final cursor) |
partial | Include a partial view with model | @await Html.PartialAsync("_PartialName", model) | $1 (PartialName), $2 (model) |
input | Create a Razor input field | $1 (Property) |
Requirements
- Visual Studio Code 1.60.0 or higher
- .NET SDK 6.0 or higher
Version History
See CHANGELOG.md for a detailed list of changes and version history.
License
MIT License - see LICENSE file for details