Skip to content

Instantly share code, notes, and snippets.

@sharwell
Last active April 3, 2022 14:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save sharwell/cb8e5bfd2c1435936c27 to your computer and use it in GitHub Desktop.
Save sharwell/cb8e5bfd2c1435936c27 to your computer and use it in GitHub Desktop.
C# Code Snippets

C# Code Snippets

This is a collection of custom C# code snippets which I use on a regular basis.

Snippet Summary

Shortcut Name Description
bf Backing field comment Insert an XML documentation comment for the backing field for a property.
ceb Contract end contract block Emits a Contract.EndContractBlock() statement.
sctor Deserialization constructor Insert a constructor for deserializating an ISerializable object.
jctor JSON constructor Insert a constructor for deserializating a JSON object.
tlz Throw if argument is less than zero Throw an ArgumentOutOfRangeException if the specified argument is less than zero.
tan Throw if argument is null Throw an ArgumentNullException if the specified argument is null.
tasn Throw if string argument is null or empty Throw an ArgumentException if the specified string argument is null or empty.
ct CancellationToken Parameter Insert a declaration for a CancellationToken parameter or variable.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Backing Field Comment</Title>
<Author>Sam Harwell</Author>
<Description>Insert an XML documentation comment for the backing field for a property.</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>bf</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>PropertyName</ID>
<ToolTip>PropertyName</ToolTip>
<Default>PropertyName</Default>
<Function></Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[/// <summary>
/// This is the backing field for the <see cref="$PropertyName$"/> property.
/// </summary>$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>CancellationToken Parameter</Title>
<Author>Sam Harwell</Author>
<Description>Insert a declaration for a CancellationToken parameter or variable.</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>ct</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>CancellationToken</ID>
<ToolTip>CancellationToken</ToolTip>
<Default>CancellationToken</Default>
<Function>SimpleTypeName(global::System.Threading.CancellationToken)</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[$CancellationToken$ cancellationToken$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Contract End Contract Block</Title>
<Author>Sam Harwell</Author>
<Description>Emits a 'Contract.EndContractBlock' statement</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>ceb</Shortcut>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace>System.Diagnostics.Contracts</Namespace>
</Import>
</Imports>
<Code Language="csharp"><![CDATA[Contract.EndContractBlock();$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Deserialization constructor</Title>
<Author>Sam Harwell</Author>
<Description>Insert a constructor for deserializating an ISerializable object.</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>sctor</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>classname</ID>
<ToolTip>classname</ToolTip>
<Default>classname</Default>
<Function>ClassName()</Function>
</Literal>
<Literal Editable="false">
<ID>SerializationInfo</ID>
<ToolTip>SerializationInfo</ToolTip>
<Default>SerializationInfo</Default>
<Function>SimpleTypeName(global::System.Runtime.Serialization.SerializationInfo)</Function>
</Literal>
<Literal Editable="false">
<ID>StreamingContext</ID>
<ToolTip>StreamingContext</ToolTip>
<Default>StreamingContext</Default>
<Function>SimpleTypeName(global::System.Runtime.Serialization.StreamingContext)</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[protected $classname$($SerializationInfo$ info, $StreamingContext$ context)
: base(info, context)
{
}]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>JSON constructor</Title>
<Author>Sam Harwell</Author>
<Description>Insert a constructor for deserializating a JSON object.</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>jctor</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>classname</ID>
<ToolTip>classname</ToolTip>
<Default>classname</Default>
<Function>ClassName()</Function>
</Literal>
<Literal Editable="false">
<ID>JsonConstructor</ID>
<ToolTip>JsonConstructor</ToolTip>
<Default>JsonConstructor</Default>
<Function>SimpleTypeName(global::Newtonsoft.Json.JsonConstructorAttribute)</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[/// <summary>
/// Initializes a new instance of the <see cref="$classname$"/> class
/// during JSON deserialization.
/// </summary>
[$JsonConstructor$]
protected $classname$()
{
}$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Throw if argument less than zero</Title>
<Author>Sam Harwell</Author>
<Description>Throw an ArgumentOutOfRangeException if the specified argument is less than zero.</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>tlz</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>arg</ID>
<ToolTip>arg</ToolTip>
<Default>arg</Default>
<Function>
</Function>
</Literal>
<Literal Editable="false">
<ID>ArgumentOutOfRangeException</ID>
<ToolTip>ArgumentOutOfRangeException</ToolTip>
<Default>ArgumentOutOfRangeException</Default>
<Function>SimpleTypeName(global::System.ArgumentOutOfRangeException)</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[if ($arg$ < 0)
throw new $ArgumentOutOfRangeException$("$arg$");$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Throw if argument null</Title>
<Author>Sam Harwell</Author>
<Description>Throw an ArgumentNullException if the specified argument is null.</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>tan</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>arg</ID>
<ToolTip>arg</ToolTip>
<Default>arg</Default>
<Function>
</Function>
</Literal>
<Literal Editable="false">
<ID>ArgumentNullException</ID>
<ToolTip>ArgumentNullException</ToolTip>
<Default>ArgumentNullException</Default>
<Function>SimpleTypeName(global::System.ArgumentNullException)</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[if ($arg$ == null)
throw new $ArgumentNullException$("$arg$");$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Throw if string argument null or empty</Title>
<Author>Sam Harwell</Author>
<Description>Throw an ArgumentException if the specified string argument is null or empty.</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>tasn</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>arg</ID>
<ToolTip>arg</ToolTip>
<Default>arg</Default>
<Function>
</Function>
</Literal>
<Literal Editable="false">
<ID>ArgumentException</ID>
<ToolTip>ArgumentException</ToolTip>
<Default>ArgumentException</Default>
<Function>SimpleTypeName(global::System.ArgumentException)</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[if (string.IsNullOrEmpty($arg$))
throw new $ArgumentException$("$arg$ cannot be empty", "$arg$");$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment