I have an object with a custom WriteLine(string)
method. Something like this:
public void WriteLine(string text) { this.StringList.Add(text); }
What is the easiest way to duplicate the functionality of string.Format()
with this method? For example: What I currently often find myself doing is this:
myObj.WriteLine(string.Format("Hello, {0}", name));
If you create a new Console application their version of a WriteLine()
method does exactly what I would prefer to do:
Console.WriteLine("Hello, {0}", name);
They eliminate the need to call string.Format()
. Is it easy to make your method accept this somehow? Or am I going to have to create a thousand method overloads? Something like this:
public void WriteLine() { ... } public void WriteLine(string text) { ... } public void WriteLine(string text, object arg0) { ... } public void WriteLine(string text, object arg0, object arg1) { ... } public void WriteLine(string text, object arg0, object arg1, object arg2) { this.StringList.Add(string.Format(text, arg0, arg1, arg2)); } // etc etc etc
Is that the only logical way to do this? Any suggestions are welcomed :)
In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.
format() method returns the formatted string by a given locale, format, and argument. If the locale is not specified in the String. format() method, it uses the default locale by calling the Locale.
You need to copy the method signature from string.format.
public void WriteLine(string text,params object[] args) { this.StringList.Add(string.Format(text,args)); }
As suggested by ChaosPandion you can also include overloads to prevent array creation
public void WriteLine(string text) { this.StringList.Add(text); } public void WriteLine(string text,object arg0) { this.StringList.Add(string.Format(text, arg0)); } public void WriteLine(string text,object arg0, object arg1) { this.StringList.Add(string.Format(text, arg0, arg1)); } public void WriteLine(string text,object arg0, object arg1, object arg2) { this.StringList.Add(string.Format(text, arg0, arg1, arg2)); }
I wouldn't go past arg2 as string.format doesn't so the benefit disappears.
There is one edge case you might want to take pains to avoid. The following code will write the string {0} to the console
Console.WriteLine("{0}");
However, you use one of the implementations of WriteLine suggested by others in this answer, those implementations will throw an exception:
WriteLine("{0}"); //throws exception
The easiest fix is either to have two overloads of WriteLine or to modify the suggested code slightly to handle the edge case:
public void WriteLine(string text,params object[] args) { var message=args.Length==0 ? text : string.Format(text, args); this.StringList.Add(message); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With