开发者

How is String.Format method treated?

As is described at Microsoft Site ,String.Format arranges some String Variables into a single String and is another way how to concate string's in c#.

string.Format("Name = {0} Surname = {1}",name,surname);

My question is how does this work ? Is this method a Special method ,and can i create a method similar to 开发者_如何转开发this one which accepts at every {n} only an Integer . Or is this method interpreted in a different way by compiler ,if yes than how does the compiler accesses this method .

If it's possible i would like to see ,how does the compiler interpret this Method.

PS : How does it work ,when you can send as much parameters as you want to a method ?

[EDIT] Does it mean ,that String.Format takes the first Parameter and filter's into with a Regex or Whatever tool (Split etc) to get where {Number} is and places there a String token from second params portion ?


Sending a variable number of parameters to a method is done like this:

public static string MyStringFormat(string formatString, params object [] args)
{

}

You can now pass as many parameters as you like to this:

MyStringFormat("{0}{1}",42,"Hello World")
MyStringFormat("{0}{1}{2}",42,"Hello World",999.9)

Within the method, these arguments are simply an array (of object in this case). here are the docs on the params keyword.

As for writing you own method to accept numeric input like Format does, this would be one way (using regular expressions):

 public static string MyStringFormat(string formatString, params object[] args)
    {
        var regex = new Regex("{([0-9]*)}");
        return regex.Replace(formatString,m =>
                           {
                               int index = int.Parse(m.Groups[1].Value);
                               if(index<args.Length)
                                  return args[index].ToString();
                               return String.Empty;
                           });
    }

Live example: http://rextester.com/rundotnet?code=OMZC13551


It is a normal function.

It parses the string and calls ToString on the incoming parameters (if needed) in order to construct the new string.

The signature of the overload you have in your example is:

public static string Format(
    string format,
    params Object[] args
)

There is nothing to stop you from creating your own (though I would probably just delegate to the built in string.Format).


Use params: http://msdn.microsoft.com/en-us/library/w5zay9db.aspx


there is no magic there Cody, look at this method:

public static void UseParams(params int[] list) 
    {
        for (int i = 0 ; i < list.Length; i++)
        {
            Console.WriteLine(list[i]);
        }
        Console.WriteLine();
    }

you can call it passing as many int you like, it's the params parameter type :)

Edit:

params is very useful it allows us, for example, to pass to our logging wrapper all method's parameters values in a dynamic way without knowing how many of them each method has, the logger will then dump all those values one by one using Reflection to find out the parameter name as well.


The braces in the string are placeholders and a number within that denotes the index of argument it is to be replaced with.

Format is of type (string format, params object[] args) and it is the index in this object[] which is helpful.

(I think it internally uses the StringBuilder.AppendFormat) for substituting those values)


The signature of String.Format looks like this:

public static string Format (string format, params Object[] args)

Note the second parameter, marked with the params keyword. That one provides the functionality to add multiple parameters when calling the function. Inside the function you can access the parameters by index or iterating over args array:

public static string Format (string format, params Object[] args)
{
    foreach (Object o in args)
        Console.WriteLine(o);
}

Have a look at the C# Reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜