Reputation: 6450
Is there any side effect of passing and extra argument to the string.Format
function in C#? I was looking at the string.Format
documentation at MSDN but was unable to find an answer.
E.g.:
string str = string.Format("Hello_{0}", 255, 555);
Now, as you can see in the according to format string, we are supposed to pass only one argument after it, but I have passed two.
I have tried it on my end and everything looks fine to me, but I just want to make sure that it will not cause any problems in later runs.
Upvotes: 7
Views: 2919
Reputation: 2700
If you look at the exception section of the link you provide for string.Format
"The index of a format item is less than zero, or greater than or equal to the length of the args array."
Microsoft doesn't indicate that it can throw if you have too much arguments, so it won't. The effect is a small loss of memory due to an useless parameter
Upvotes: 3
Reputation: 50104
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.
Upvotes: 8