Hey,
I have a question concerning implementing a suitable ToString() functionality.
1. Implement the IConvertible interface.
2. Using Override
What is the difference between the two methods?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sunny ChenPosted Jul 3, 2008, 8:54 PM
I think if you implement IConvertable, you should firstly be sure that your instance of the class will have the ability to be converted to integer, boolean, string or other built-in primitive types. In such case, user of your class library may use the Convert class to convert your object into various data types, like following:
int i = Convert.ToInt32(objYourObject);
However if you override the ToString() method which was defined in the object class, you may simply get a "string name" of your object. Maybe the object cannot be converted into integer, boolean or other primitive types, but you can override the ToString() method to give the user a human-readable string which identifies the object.
So which way to go should depend on the requirement. I don't know if I explained clearly.
Hope this helps!
Niradhip ChakrabortyPosted Jul 1, 2008, 3:08 AM
Please check out the following article:
http://www.tanguay.info/webold/codeExample.php5?id=781