2

C# in VS2005: can you overload the .ToString() method of a property?

1 Answer 1

5

No - properties have a get method, which returns a value -- you can overload the implementation of ToString() on the type of that value, which will allow you to do:

myClass.MyProperty.ToString()

which is likely what you're looking for. Simply define a function such as:

public override string ToString() {
    // return string version of value
}

in that type's class.

1
  • That is unfortunate. A Property is like a wrapper object for a type, so I think you should be able to overload methods like ToString() to customise the string representation of a Property.
    – CJ7
    Jul 20, 2010 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.