cosa fa questo pezzo di codice string.format?
-
22-09-2019 - |
Domanda
Ho questo pezzo di codice 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()));
}
Sto cercando di capire quale parte inizia con "getColumnName?" e termina con ".ToString ()".Ho capito che è un tipo system.object, ma non ho idea di cosa faccia specificamente o di come funzioni.Lo voglio per questo motivo:"reader" conteneva più righe e voglio scrivere solo righe specifiche.
Se qualcuno può aiutarmi su uno di questi, gliene sarei grato.
Soluzione
La funzione esegue l'iterazione su tutte le colonne nel lettore dati, quindi per ciascuna di esse:
Se getColumnName
restituisce true, restituisce il nome della colonna tra i <td>
tag, altrimenti il valore dei dati.
Per decostruire ulteriormente:
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)
Altri suggerimenti
Questa è una operatore condizionale . Si dice che se getColumnName
è vero, quindi utilizzare reader.GetName(i)
altrimenti utilizzare reader.GetValue(i).ToString()
Il formato è questo:
ThingToCheck ? UseIfCheckIsTrue : UseIfCheckIsFalse
Nel codice, sembra che getColumnName
è true per la riga di intestazione, quindi è in uscita il nome della colonna e chiamato nuovamente per tutte le altre righe utilizzando false , all'uscita i valori.
Questo si chiama un operatore condizionale.
Il getColumnName
argomento viene valutato e, se vera, viene restituito il primo argomento dopo il ?
, se falsa, la seconda.
Quindi, se getColumnName == true, si sta andando a vedere <td>NAME</td>
altro <td>Value</td>
Dare un senso?
E 'come segue
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()
}
A causa getColumnName è di tipo bool quindi nessun bisogno di testarlo come
If (getColumnName == true)
È possibile scrivere questo come
If (getColumnName)
String.Format (stringa, metodo)
E metodo String.Format sostituisce i punti in stringa specificata con oggetto dato, questo metodo ha due argomenti primo è corda e secondo è oggetto. ad esempio
string.Format("Question number {0} is answered by {1} {2}", 11, "Adam", "Yesterday");
L'messo fuori sarà
Domanda numero 11 si risponde con Adam Ieri
Come si può vedere {0} è sostituito con 11 e {1} viene sostituito con Adam e {2} viene sostituito con ieri.
si può leggere di più su questo qui
operatore ternario , usato per adhoc costituzione di blocco if altro.