I need to make sure that a datatype implements the IComparable interface, and I was wondering if there was anyway to make that a requirement when you create the object?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can perhaps use generic to do this, for example:
Or if you mean ‘when you create the type’ (in code), then no; you’d just have to remember, perhaps using unit-tests to verify that you’ve done it.
I recommend using the typed
IComparable<T>overIComparable– it makes life a lot easier (and avoids some boxing, but that is less of an issue). Finally, remember that you can useComparer<T>.DefaultandComparer.Defaultin code if you want to duck-type the comparable bit (like howList<T>.Sort()works).