i would like to override the ToString() method for an int (that is a part of a class) so that if the value of the int is 0, the ToString() should return an empty string "".Can this be done?
UPDATE
It would be easy to just create a
public string AmountToString {
get { if (Amount != 0) return Amount.ToString(); else return ""; }
}
i was just curious to see if it could be implemented (the ToString() ) on an primite type
Three main approaches:
Employ a (custom) Format provider
So you can use i.ToString(formatprovider);
Edit I think I found a custom format string one that works with the default .NET number format provider:
Another sample lifted from the page on The “;” Section Separator:
Extension method
Accessor Helper: