Switch Statement and Expression
Statement
public string SetFileIcon(string fileName)
{
switch (Path.GetExtension(fileName))
{
case "jpg":
case "jpeg":
case "png":
case "gif":
return "image";
case "html":
return "code";
case "docx":
return "word";
case "pdf":
return "pdf";
default:
return "";
}
}
Expression
public string SetFileIcon(string fileName)
{
return Path.GetExtension(fileName) switch
{
"jpg" or "jpeg" or "png" or "gif" => "image",
"html" => "code",
"docx" => "word",
"pdf" => "pdf",
_ => "",
};
}