C# Snippets
Editor shortcuts for Visual Studio and VS Code. Each snippet maps to a feature you can learn in depth on this site.
Async/Await Method
csasync
Declare an async method using async/await (C# 5.0+)
public async Task<${1:string}> ${2:GetDataAsync}()
{
var result = await ${3:FetchAsync()};
return result;
}
Collection Expressions
cscollexpr
Initialize a collection using the bracket syntax (C# 12.0+)
${1:int}[] ${2:values} = [${3:1, 2, 3}];
Default Interface Members
csdefaultimpl
Define an interface with a default method implementation (C# 8.0+)
public interface ${1:IFormatter}
{
string ${2:Format}(double value) => value.ToString("0.00");
}
Extension Methods
csextension
Define a static extension method class (C# 3.0+)
public static class ${1:StringExtensions}
{
public static bool ${2:IsNullOrEmpty}(this string ${3:value}) => string.IsNullOrEmpty(${3:value});
}
File-Scoped Namespace
csfilenamespace
Declare a file-scoped namespace to reduce indentation (C# 10.0+)
namespace ${1:MyApp.Services};
public class ${2:OrderService}
{
}
Func and Action Delegates
csfunc
Declare Func and Action delegate variables (C# 3.0+)
Func<int, int, int> ${1:add} = (a, b) => a + b;
Action<string> ${2:print} = message => Console.WriteLine(message);
Global Using Directives
csglobalusing
Declare global using directives for project-wide imports (C# 10.0+)
global using System;
global using System.Collections.Generic;
global using System.Linq;
Init Accessors
csinit
Declare a class with init-only property setters (C# 9.0+)
public class ${1:Point}
{
public int X { get; init; }
public int Y { get; init; }
}
Lambda Expressions
cslambda
Declare a lambda expression using Func delegate (C# 3.0+)
Func<int, bool> ${1:isEven} = ${2:n} => ${3:n % 2 == 0};
LINQ Query
cslinq
Query a collection using LINQ method syntax (C# 3.0+)
var ${1:results} = ${2:items}
.Where(x => x.IsActive)
.OrderBy(x => x.Name)
.Select(x => x.Name)
.ToList();
List Patterns
cslistpattern
Match sequences using list patterns in a switch expression (C# 11.0+)
string ${1:label} = ${2:values} switch
{
[var first, ..] => $"Starts with {first}",
[] => "Empty",
_ => "Other"
};
Nullable Reference Types
csnullable
Declare and null-check a nullable reference type variable (C# 8.0+)
string? ${1:name} = ${2:GetName()};
if (${1:name} is not null)
{
Console.WriteLine(${1:name}.Length);
}
Use an out parameter with TryParse (C# 7.0+)
if (int.TryParse(${1:input}, out int ${2:value}))
{
Console.WriteLine(${2:value});
}
Pattern Matching (is)
cspattern
Match a value against a type or property pattern (C# 7.0+)
if (${1:obj} is ${2:string} ${3:s})
{
$0
}
Primary Constructor
csprimary
Declare a class with a primary constructor (C# 12.0+)
public class ${1:OrderService}(${2:ILogger} ${3:logger})
{
public void Process()
{
${3:logger}.LogInformation("Processing...");
}
}
Raw String Literals
csrawstring
Declare a raw string literal for multi-line text (C# 11.0+)
var ${1:json} = """
{
"name": "value"
}
""";
Record Type
csrecord
Declare an immutable record with positional properties (C# 9.0+)
public record ${1:Person}(${2:string} ${3:FirstName}, ${4:string} ${5:LastName});
Required Members
csrequired
Class with required init-only properties (C# 11.0+)
public class ${1:UserProfile}
{
public required string ${2:Name} { get; init; }
public required string ${3:Email} { get; init; }
}
Span and ReadOnlySpan
csspan
Use ReadOnlySpan for allocation-free string slicing (C# 7.2+)
ReadOnlySpan<char> ${1:text} = "Hello, World!".AsSpan();
ReadOnlySpan<char> ${2:hello} = ${1:text}.Slice(0, 5);
Static Abstract Interface Members
csstaticabstract
Define an interface with a static abstract operator (C# 11.0+)
public interface ${1:IAddable}<${2:TSelf}> where ${2:TSelf} : ${1:IAddable}<${2:TSelf}>
{
static abstract ${2:TSelf} operator +(${2:TSelf} left, ${2:TSelf} right);
}
String Interpolation
csinterp
Embed expressions in a string using $ prefix (C# 6.0+)
var message = $"${1:Hello}, {${2:name}}!";
Switch Expression
csswitchexpr
Concise switch expression returning a value (C# 8.0+)
var ${1:result} = ${2:value} switch
{
${3:1} => "${4:one}",
${5:2} => "${6:two}",
_ => "${7:other}"
};
Top-Level Statements
cstoplevel
Write a program using top-level statements without a Main method (C# 9.0+)
using System;
Console.WriteLine("${1:Hello, World!}");
Tuples and Deconstruction
cstuple
Return and deconstruct a named tuple (C# 7.0+)
(${1:double} ${2:X}, ${3:double} ${4:Y}) ${5:GetPoint}()
{
return (${6:0.0}, ${7:0.0});
}
var (${8:x}, ${9:y}) = ${5:GetPoint}();
Using Declaration
csusingdecl
Declare a disposable resource that is automatically disposed at end of scope (C# 8.0+)
using var ${1:resource} = new ${2:StreamReader}(${3:"file.txt"});
Declare a local variable using type inference (C# 3.0+)
var ${1:name} = ${2:value};