这段 string.format 代码是做什么的?
我在 C# 中有这段代码:
private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter stwr, bool getColumnName)
{
for (int i = 0; i < reader.FieldCount; i++)
stwr.Writeline(String.Format("<td>{0}</td"), getColumnName ? reader.GetName(i) : reader.GetValue(i).ToString()));
}
我试图理解以“getColumnName”开头的部分是什么?并以“.ToString()”结尾。我知道它是一个 system.object 类型,但我不知道它具体做什么或如何工作。 我想要这样是因为:“reader”中有多行,并且我只想只写入特定行。
如果有人能在这两方面帮助我,我将不胜感激。
I have this piece of code in c#:
private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter stwr, bool getColumnName)
{
for (int i = 0; i < reader.FieldCount; i++)
stwr.Writeline(String.Format("<td>{0}</td"), getColumnName ? reader.GetName(i) : reader.GetValue(i).ToString()));
}
I'm trying to understand what the part that start with "getColumnName ?" and ends with ".ToString()" does. I understood that it is a system.object type, but I have no idea what it specifically does or how it works.
I want that because of this: "reader" had multiple rows in it, and I want to writeline only specific rows.
If anyone can help me on either of those, I'd be grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个条件运算符 。它表示如果
getColumnName
为 true,则使用reader.GetName(i)
否则使用reader.GetValue(i).ToString()
格式是这样的:
在代码中,对于标题行,看起来
getColumnName
为 true,因此它输出列名称,并使用 false 再次调用所有其他行,输出值。This is a conditional operator. It says if
getColumnName
is true, then usereader.GetName(i)
otherwise usereader.GetValue(i).ToString()
The format is this:
In the code, it looks like
getColumnName
is true for the header row, so it's outputting the column name and called again for all other rows using false, to output the values.该函数迭代数据读取器中的所有列,然后针对每一列:
如果
getColumnName
返回 true,则它会输出标记之间的列名称,否则为数据值。
进一步解构:
The function iterates over all columns in the data reader, then for each one:
If
getColumnName
returns true, it outputs the name of the column between the<td>
tags, otherwise the value of the data.To de-construct further:
这就是所谓的条件运算符。
计算参数
getColumnName
,如果为 true,则返回?
后的第一个参数,如果为 false,则返回第二个参数。因此,如果 getColumnName==true,您将看到
NAME
elseValue
有意义吗?
That is called a conditional operator.
The argument
getColumnName
is evaluated and if true, the first argument after the?
is returned, if false, the second.So, if getColumnName==true, you are going to see
<td>NAME</td>
else<td>Value</td>
Make sense?
如下所示
因为 getColumnName 是 bool 类型,所以不需要像这样测试它
您可以将其写为
String.Format(string, method)
并且 String.Format 方法用给定对象替换指定字符串中的项目,该方法有两个参数,第一个是字符串,第二个是对象。
例如,
输出将是
Adam Yesterday 回答的问题编号 11
如您所见,{0} 被替换为 11,{1} 被替换为 Adam,{2} 被替换为 Yesterday。
您可以在此处阅读有关此内容的更多信息
It is like following
Because getColumnName is of bool type so no need to test it like
You can write this as
String.Format(string, method)
And String.Format method replaces items in specified string with given object, this method has two arguments first one is string and second is object.
for example
The out put will be
Question number 11 is answered by Adam Yesterday
As you can see {0} is replaced with 11 and {1} is replaced with Adam and {2} is replaced with Yesterday.
you can read more about this here
这是三元运算符,用于临时构成if else 块。
this is ternary operator, used for adhoc constitution of if else block.