E.g. in the following example:
string commandText = string.Format("Select * from {0}", filename);
How does the above work?
{0}
is a placeholder for the first object given; in this case that's filename
, so it will insert whatever filename evaluates to in place of {0}
. Similarly, of course you could use {1}
and that would be replaced with the second parameter passed, etc.
{0}
refers to the second parameter passed into String.Format
. {1}
refers to the third, {2}
to the fourth, etc. For example:
String.Format("The {0} brown {1} jumps {2} the {3} dog.", "quick", "fox", "over", "lazy")
Evaluates to
"The quick brown fox jumps over the lazy dog."
It means the value of filename. Please see MSDN.
It is an indexer to the arguments presented after the “Select * from {0}”
and can be combined with format specifiers as well. See the documentation for String.Format Method. However, you should NEVER EVER create a SQL command this way as it is vulnerable to SQL Injection attacks. You should always parameterize SQL queries. See the How To: Protect From SQL Injection in ASP.NET article on MSDN.
The {0}
is a reference to the first argument passed after the format string.
In your case, "Select * from {0}"
is the format string and filename
is the first argument.
As an example:
String.Format("Select * from {0}{1}{0}","this","database")
would return:
"Select * from thisdatabasethis"
来源:https://stackoverflow.com/questions/9625750/what-does-0-mean-in-string-format