Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing extra argument to string.Format() function in C#

Tags:

c#

Is there any side effect of passing and extra argument to string.Format function in C#? I was looking at the string.Format function documentation at MSDN ( http://msdn.microsoft.com/en-us/library/b1csw23d.aspx) but unable to find an answer.

Eg:-

string str = string.Format("Hello_{0}", 255, 555);

Now, as you can see that according to format string, we are suppose to pass only one argument after it but I have passed two.

EDIT: I have tried it on my end and everything looks fine to me. Since I am new to C# and from C background, I just want to make sure that it will not cause any problem in later run.

like image 399
Ravi Gupta Avatar asked Nov 30 '12 12:11

Ravi Gupta


1 Answers

Looking in Reflector, it will allocate a little more memory for building the string, but there's no massive repercussion for passing in an extra object.

There's also the "side effect" that, if you accidentally included a {n} in your format string where n was too large, and then added some spare arguments, you'd no longer get an exception but get a string with unexpected items in.

like image 155
Rawling Avatar answered Oct 09 '22 12:10

Rawling