这段 string.format 代码是做什么的?

发布于 2024-08-26 16:17:26 字数 494 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柒夜笙歌凉 2024-09-02 16:17:26

这是一个条件运算符 。它表示如果 getColumnName 为 true,则使用 reader.GetName(i) 否则使用 reader.GetValue(i).ToString()

格式是这样的:

ThingToCheck ? UseIfCheckIsTrue : UseIfCheckIsFalse

在代码中,对于标题行,看起来 getColumnNametrue,因此它输出列名称,并使用 false 再次调用所有其他行,输出值。

This is a conditional operator. It says if getColumnName is true, then use reader.GetName(i) otherwise use reader.GetValue(i).ToString()

The format is this:

ThingToCheck ? UseIfCheckIsTrue : UseIfCheckIsFalse

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.

梦里南柯 2024-09-02 16:17:26

该函数迭代数据读取器中的所有列,然后针对每一列:

如果 getColumnName 返回 true,则它会输出 标记之间的列名称,否则为数据值。

进一步解构:

reader.GetName(i) - this returns the name of the column

reader.GetValue(i).ToString() - this returns the value of the column as a string

getColumnName - a function the will return true if a column name can be gotten

?: - the conditional operator. If the expression before the ? is true, the expression to the left of the : is used, otherwise the one on the right

String.Format("<td>{0}</td", arg) - this will output "<td>arg</td>" (btw - your code is wrong, the ) should not be just after the first string)

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:

reader.GetName(i) - this returns the name of the column

reader.GetValue(i).ToString() - this returns the value of the column as a string

getColumnName - a function the will return true if a column name can be gotten

?: - the conditional operator. If the expression before the ? is true, the expression to the left of the : is used, otherwise the one on the right

String.Format("<td>{0}</td", arg) - this will output "<td>arg</td>" (btw - your code is wrong, the ) should not be just after the first string)
请你别敷衍 2024-09-02 16:17:26

这就是所谓的条件运算符。

计算参数 getColumnName,如果为 true,则返回 ? 后的第一个参数,如果为 false,则返回第二个参数。

因此,如果 getColumnName==true,您将看到 NAME else Value

有意义吗?

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?

乖乖公主 2024-09-02 16:17:26

如下所示

if (getColumnName == true)
{
    reader.GetName(i); // GetName is string so no need to convert this in string I.E ToString()
}
else
{
    reader.GetValue(i).ToString(); // GetValue returns object so this needs to convert in string using .ToString()
}

因为 getColumnName 是 bool 类型,所以不需要像这样测试它

If (getColumnName == true)

您可以将其写为

If (getColumnName)

String.Format(string, method)

并且 String.Format 方法用给定对象替换指定字符串中的项目,该方法有两个参数,第一个是字符串,第二个是对象。
例如,

string.Format("Question number {0} is answered by {1} {2}", 11, "Adam", "Yesterday");

输出将是

Adam Yesterday 回答的问题编号 11

如您所见,{0} 被替换为 11,{1} 被替换为 Adam,{2} 被替换为 Yesterday。

您可以在此处阅读有关此内容的更多信息

It is like following

if (getColumnName == true)
{
    reader.GetName(i); // GetName is string so no need to convert this in string I.E ToString()
}
else
{
    reader.GetValue(i).ToString(); // GetValue returns object so this needs to convert in string using .ToString()
}

Because getColumnName is of bool type so no need to test it like

If (getColumnName == true)

You can write this as

If (getColumnName)

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

string.Format("Question number {0} is answered by {1} {2}", 11, "Adam", "Yesterday");

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

云胡 2024-09-02 16:17:26

这是三元运算符,用于临时构成if else 块。

this is ternary operator, used for adhoc constitution of if else block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文