English 中文(简体)
what does this string.format piece of code do?
原标题:

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.

最佳回答

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)
问题回答

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.

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?

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

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签