Referring to an item in an IEnumerable with no ID
So I may have coded myself into a corner, and I want to know the best way out.
I have this document editor I'm writing, and one property of the documents being edited is a list of structures. The document is stored as XML, so each of these structures is an XML node and its properties. My Document
class exposes these structures as an IEnumerable.
In my editor, I want to literally highlight these structures when the mouse is nearby. I've already done t开发者_StackOverflowhe task of identifying one close to the cursor. But now I have to be able to refer to that instance of the structure, and store that somewhere. Finding the closest one just iterates through the IEnumerable, and returns the structure itself. I suppose that I could use the structure itself as the reference, but then I'm going to wind up saying in my display code if (thing == nearestThing)
and it's going to do a hash code comparison or something, right?
That feels like the wrong way to do it, but I don't have a proper ID for these structures either. Suggestions?
There is no problem with that way. Keep in mind though, you should make sure that ==
(and to a greater extend, Equals
and GetHashcode
) reliably produce the same results for the same inputs.
精彩评论