C# Sorted List by Value with Object

前端 未结 9 925
失恋的感觉
失恋的感觉 2021-01-21 20:06

I\'m trying to create an \"ordered\" cache of objects in C#, where the order is determined by how many times that has been accessed.

I\'ve looked into Dictionary, Sorted

9条回答
  •  星月不相逢
    2021-01-21 20:31

    The "Proper" way to do this is to implement IComparable (http://msdn.microsoft.com/en-us/library/system.icomparable.aspx) Interface in your MyCache Class.

    This will expose a method called CompareTo which you will have to write in your code.

    You would just create that method and put some logic in there that says if this object is greater, less than or equal to the object passed in.

    Then you use it in your client code by saying int result = MyCache1.ComparTo(MyCache2);

    The result will be -1 0 or 1 based on if its greater than less than or equal too.

提交回复
热议问题