Skip to content

Instantly share code, notes, and snippets.

@kei10in
Created November 18, 2015 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kei10in/5b90c03456212c20b175 to your computer and use it in GitHub Desktop.
Save kei10in/5b90c03456212c20b175 to your computer and use it in GitHub Desktop.
Code snippet to implement operator == in C# for Visual Studio
<?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>OperatorEquals</Title>
<Author>kei10in@gmail.com</Author>
<Description>Code Snippet to implement operator == under guildline</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>eq</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>classname</ID>
<ToolTip>Class name</ToolTip>
<Default>ClassNamePlaceholder</Default>
<Function>ClassName()</Function>
</Literal>
<Literal Editable="false">
<ID>Exception</ID>
<ToolTip>
</ToolTip>
<Default>
</Default>
<Function>SimpleTypeName(global::System.NotImplementedException)</Function>
</Literal>
</Declarations>
<Code Language="csharp" Delimiter="$"><![CDATA[public override bool Equals(object obj)
{
return Equals(obj as $classname$);
}
public bool Equals($classname$ rhs)
{
return this == rhs;
}
public override int GetHashCode()
{
// TODO: write your implementation of GetHashCode() here
throw new $Exception$();
return base.GetHashCode();
}
public static bool operator ==($classname$ v1, $classname$ v2)
{
if (ReferenceEquals(v1, v2)) return true;
if ((object)v1 == null || (object)v2 == null) return false;
throw new System.NotImplementedException();$end$
}
public static bool operator !=($classname$ v1, $classname$ v2)
{
return !(v1 == v2);
}]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment