IPAddress and IComparabe<T>

Can anybody explain the reason why IPAddress does not implement IComparable interface I wanna use IPAddress in my binary search tree and using the GetBytes() method of the IPAddress for comparing seems to me very inefficient. Any alternatives


Answer this question

IPAddress and IComparabe<T>

  • IDDesigns

    Maksim,
    I can't tell for sure, but since there isn't a "natural" ordering of IP addresses, any implementation of IComparable would have been arbitrary. For instance, you might use network order, or host order with much different results.

    Instead of GetBytes you might want to use GetHashCode, which not surprisingly returns an int representing the IP Address (you will get it reversed, so that 127.0.0.1 will become 0x0100007f).

    While you are at it, if your only goal is to look for an address in some collection, and performance is a concern, you may want to drop the binary search and go with a HashTable instead.

    HTH
    --mc


  • Leonnuel

    With thousands of IP addresses in the collection, memory is very precious and that's why I wanna use balanced trees. I could implement my own comparer if IPAddress class had its IP array explosed to the derived classes. Anyway, thanks for reminding about hashtables.



  • IPAddress and IComparabe<T>