Last active
December 11, 2015 07:28
-
-
Save martijnhoekstra/4566337 to your computer and use it in GitHub Desktop.
hashset
This file contains 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
static void Main(string[] args) | |
{ | |
bool success = false; | |
HashSet<DataObject> set = new HashSet<DataObject>(); | |
DataObject myDataObject = new DataObject { foo = 1, bar = "bar" }; | |
success = set.Add(myDataObject); | |
Console.WriteLine(set.Contains(myDataObject)); | |
myDataObject.foo = 2; | |
Console.WriteLine(set.Contains(myDataObject)); | |
DataObject taken = set.First(); | |
Console.WriteLine(taken.Equals(myDataObject)); | |
Console.WriteLine(set.Contains(taken)); | |
Console.WriteLine(ReferenceEquals(taken, myDataObject)); | |
success = set.Add(taken); | |
if (success) Console.Write("successfully "); else Console.Write("unsuccessfully "); | |
Console.WriteLine("Added taken"); | |
Console.WriteLine(set.Count); | |
foreach (DataObject obj in set) | |
{ | |
Console.WriteLine(obj.ToString()); | |
} | |
success = set.Add(new DataObject { foo = 1, bar = "bar" }); | |
if (success) Console.Write("successfully "); else Console.Write("unsuccessfully "); | |
Console.WriteLine("Added new DataObject"); | |
foreach (DataObject obj in set) | |
{ | |
Console.WriteLine(obj.ToString()); | |
} | |
success = set.Add(new DataObject { foo = 1, bar = "bar" }); | |
if (success) Console.Write("successfully "); else Console.Write("unsuccessfully "); | |
Console.WriteLine("Added new DataObject again"); | |
foreach (DataObject obj in set) | |
{ | |
Console.WriteLine(obj.ToString()); | |
} | |
success = set.Remove(taken); | |
if (success) Console.Write("successfully "); else Console.Write("unsuccessfully "); | |
Console.WriteLine("removed taken from set"); | |
foreach (DataObject obj in set) | |
{ | |
Console.WriteLine(obj.ToString()); | |
} | |
success = set.Remove(taken); | |
if (success) Console.Write("successfully "); else Console.Write("unsuccessfully "); | |
Console.WriteLine("removed taken from set"); | |
foreach (DataObject obj in set) | |
{ | |
Console.WriteLine(obj.ToString()); | |
} | |
Console.WriteLine(set.Contains(taken)); | |
Console.WriteLine(set.Count); | |
Console.ReadLine(); | |
} | |
} | |
public class DataObject | |
{ | |
public int foo { get; set; } | |
public string bar { get; set; } | |
public override int GetHashCode() | |
{ | |
return foo.GetHashCode() ^ bar.GetHashCode(); | |
} | |
public override bool Equals(object obj) | |
{ | |
DataObject that = obj as DataObject; | |
return (that != null && this.foo == that.foo && String.Equals(this.bar, that.bar)); | |
} | |
public override string ToString(){ | |
return String.Format("foo : {0}, bar : {1}", this.foo, this.bar); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment