Trying to use C# to print a string that has a parameter while also testing if it exists
string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "{0} exists.", curFile : "File does not exist.");
It came back with an error though. What would be the correct way of writing this?
Looks like you're using string.Format syntax inside a ternary.
You can either supply the string.Format wrapper.
Console.WriteLine(
File.Exists(curFile)
? string.Format("{0} exists.", curFile)
: "File does not exist.");
Or go for string interpolation with a leading $ sign.
Console.WriteLine(
File.Exists(curFile)
? $"{curFile} exists."
: "File does not exist.");
this is better i think:
string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? $"{curFile} exists." : "File does not exist.");
here shows what is "$" doing.