问题
E.g. in the following example:
string commandText = string.Format("Select * from {0}", filename);
How does the above work?
回答1:
{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.
回答2:
{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."
回答3:
It means the value of filename. Please see MSDN.
回答4:
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.
回答5:
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