1 linie i stedet for if else
HejJeg har en funktion, der skriver en string til en txt fil:
[Code]
public void WriteToFile(string message, bool logWithTime)
{
StreamWriter writer = null;
try
{
writer = new StreamWriter(new FileStream(FilePath,FileMode.Append,FileAccess.Write,FileShare.Read));
if(logWithTime)
writer.WriteLine(DateTime.Now.ToString() + " : " + message);
else
writer.WriteLine(message);
writer.Flush();
writer.Close();
}
catch
{
if(writer != null)
writer.Close();
}
}
[/Code]
Er der ikke noget med, at man i stedet for min "if else" del, kan skrive de fire linier i en enkelt?
A la:
if(logWithTime)
writer.WriteLine(DateTime.Now.ToString() + " : " + message);
else
writer.WriteLine(message);
Bliver til:
writer.WriteLine(if(logWithTime)(DateTime.Now.ToString()) + " : " + message);
Hvordan er syntaksen for den slags?