Skip to content

Instantly share code, notes, and snippets.

@heaths
Last active April 8, 2021 05:19
Show Gist options
  • Save heaths/d105148428fe09a2631322b656f04ebb to your computer and use it in GitHub Desktop.
Save heaths/d105148428fe09a2631322b656f04ebb to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Enumeration-style Structure</Title>
<Keywords>
<Keyword>enum</Keyword>
</Keywords>
<Shortcut>enumStruct</Shortcut>
</Header>
<Snippet>
<Code Language="CSharp"><![CDATA[
public readonly struct $typeName$ : IEquatable<$typeName$>
{
private readonly string _value;
/// <summary>
/// Initializes a new instance of the <see cref="$typeName$"/> structure.
/// </summary>
/// <param name="value">The string value of the instance.</param>
public $typeName$(string value)
{
_value = value ?? throw new ArgumentNullException(nameof(value));
}
// TODO: Define internal or private string constants here if you want to use them in switch statements:
// internal const string s_value1 = "value1";
// TODO: Define well-known values here (use constant values from above if defined):
// public static $typeName$ Value1 { get; } = new $typeName$(s_value1);
// public static $typeName$ Value2 { get; } = new $typeName$("value2");
/// <summary>
/// Determines if two <see cref="$typeName$"/> values are the same.
/// </summary>
/// <param name="left">The first <see cref="$typeName$"/> to compare.</param>
/// <param name="right">The second <see cref="$typeName$"/> to compare.</param>
/// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</returns>
public static bool operator ==($typeName$ left, $typeName$ right) => left.Equals(right);
/// <summary>
/// Determines if two <see cref="$typeName$"/> values are different.
/// </summary>
/// <param name="left">The first <see cref="$typeName$"/> to compare.</param>
/// <param name="right">The second <see cref="$typeName$"/> to compare.</param>
/// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</returns>
public static bool operator !=($typeName$ left, $typeName$ right) => !left.Equals(right);
/// <summary>
/// Converts a string to a <see cref="$typeName$"/>.
/// </summary>
/// <param name="value">The string value to convert.</param>
public static implicit operator $typeName$(string value) => new $typeName$(value);
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => obj is $typeName$ other && Equals(other);
/// <inheritdoc/>
public bool Equals($typeName$ other) => string.Equals(_value, other._value, StringComparison.Ordinal);
/// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => _value?.GetHashCode() ?? 0;
/// <inheritdoc/>
public override string ToString() => _value;
// TODO: If you need to attach other data to the struct value, follow the pattern below:
// internal int GetNumberValue() => _value switch
// {
// s_value1 => 1,
// _ => 0,
// };
}
]]>
</Code>
<Declarations>
<Literal Editable="true">
<ID>typeName</ID>
<ToolTip>The name of the structure you want to define.</ToolTip>
</Literal>
</Declarations>
<Imports>
<Import>
<Namespace>System</Namespace>
</Import>
<Import>
<Namespace>System.ComponentModel</Namespace>
</Import>
</Imports>
</Snippet>
</CodeSnippet>
</CodeSnippets>
@heaths
Copy link
Author

heaths commented Sep 30, 2019

Typically the overhead of bucketing isn't worth it until you have sufficient number of elements. In fact, there are types in .NET that use one implementation or another as private implementation depending on a threshold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment