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",
        _ => "",
    };
}
Last updated: 6/30/2021 2:27:53 PM

Latest Updates

© 0 - 2025 - Mike Brind.
All rights reserved.
Contact me at Mike dot Brind at Outlook.com