How to imitate string.Format() in my own method?

13,652

Solution 1

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.

Solution 2

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);
}

Solution 3

You could use params:

 public void WriteLine(string text, params object[] parameters)
 {
     //..
 }
Share:
13,652
Chev
Author by

Chev

I'm a passionate developer and I love to learn. I also love to share my knowledge with others. Both of those are the primary reasons why I'm here on Stack Overflow :)

Updated on June 13, 2022

Comments

  • Chev
    Chev almost 2 years

    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 :)