Untitled
unknown
csharp
a year ago
1.6 kB
7
Indexable
using System; using System.Collections.Generic; class A { public int a; public A(int value) { this.a = value; } public override bool Equals(object other) { A other_a = (A)other; return this.a == other_a.a; } public override int GetHashCode() { return this.a; } public override String ToString() { return "a:" + this.a; } } public class Program { public static void Main() { A a = new A(1); A b = new A(2); Dictionary< A , int > d = new Dictionary< A , int >(); d.Add(a, 1); d.Add(b, 2); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine($"eq {a.Equals(b)}"); Console.WriteLine($"hash {a.GetHashCode() == b.GetHashCode()}"); foreach (KeyValuePair<A, int> kv in d) { Console.WriteLine($"key:{kv.Key}, value:{kv.Value}"); } a.a = 2; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine($"eq {a.Equals(b)}"); Console.WriteLine($"hash {a.GetHashCode() == b.GetHashCode()}"); foreach (KeyValuePair<A, int> kv in d) { Console.WriteLine($"key:{kv.Key}, value:{kv.Value}"); } Dictionary< A , int > new_d = new Dictionary< A , int >(); new_d.Add(a, 1); new_d.Add(b, 2); foreach (KeyValuePair<A, int> kv in new_d) { Console.WriteLine($"key:{kv.Key}, value:{kv.Value}"); } } }
Editor is loading...
Leave a Comment