Composite Key Dictionary

后端 未结 9 529
心在旅途
心在旅途 2020-12-02 10:18

I have some objects in List, let\'s say List and MyClass has several properties. I would like to create an index of the list based on 3 properti

9条回答
  •  不知归路
    2020-12-02 10:55

    May I suggest an alternative - a anonymous object. It's the same we use in GroupBy LINQ method with multiple keys.

    var dictionary = new Dictionary ();
    dictionary[new { a = 1, b = 2 }] = "value";
    

    It may looks strange, but I've benchmarked Tuple.GetHashCode and new{ a = 1, b = 2 }.GetHashCode methods and the anonymous objects wins on my machine on .NET 4.5.1:

    Object - 89,1732 ms for 10000 calls in 1000 cycles

    Tuple - 738,4475 ms for 10000 calls in 1000 cycles

提交回复
热议问题