Created
September 1, 2011 17:07
-
-
Save jrwren/1186660 to your computer and use it in GitHub Desktop.
immutable struct?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [DebuggerDisplay("\\{ Name = {Name}, Age = {Age} \\}")] | |
| private sealed struct X : IEquatable<X> | |
| { | |
| private readonly string name; | |
| private readonly int age; | |
| public X(string name, int age) | |
| { | |
| this.name = name; | |
| this.age = age; | |
| } | |
| public override bool Equals(object obj) | |
| { | |
| if (obj is X) | |
| return Equals((X)obj); | |
| return false; | |
| } | |
| public bool Equals(X obj) | |
| { | |
| if (!EqualityComparer<string>.Default.Equals(name, obj.name)) | |
| return false; | |
| if (!EqualityComparer<int>.Default.Equals(age, obj.age)) | |
| return false; | |
| return true; | |
| } | |
| public override int GetHashCode() | |
| { | |
| int hash = 0; | |
| hash ^= EqualityComparer<string>.Default.GetHashCode(name); | |
| hash ^= EqualityComparer<int>.Default.GetHashCode(age); | |
| return hash; | |
| } | |
| public override string ToString() | |
| { | |
| return String.Format("{{ Name = {0}, Age = {1} }}", name, age); | |
| } | |
| public string Name | |
| { | |
| get | |
| { | |
| return name; | |
| } | |
| } | |
| public int Age | |
| { | |
| get | |
| { | |
| return age; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment