c# - overriding ToString() with formating arguments -
this question has answer here:
consider scenario:
int mynumber = 10; string formattednumber = mynumber.tostring("0000"); // output "0010" ie., can format integer using tostring() in above code. have class named mynewclass in over-riding tostring(), aim formatted output using overrided tostring() method.
public class mynewclass { public int myproperty { get; set; } public mynewclass(dynamic x) { myproperty = x; } public override string tostring() { return myproperty.tostring(); } } now creating object of class as:
mynewclass p = new mynewclass(10); when called tostring() formatting wont compile:
var m = p.tostring("0000");//gives error "no overloaded method tostring() takes 1 argument." so have changed function signature following:
public override string tostring(string format) { return myproperty.tostring(format); } again result in same as: no suitable method found override
my question how can achieve overriding tostring() formating arguments
remove override keyword , you're set. you're not overriding in case since system.object has string tostring(). you're doing adding additional function call existing 1 (just numeric value types in framework itself).
Comments
Post a Comment