多个 if-else 的 switch 语句的最佳方法

Best way for a switch statement for multiple if-else

我正在尝试将大量 'if else' 转换为 switch 语句 需要一个最佳开关案例的指针,一些代码结构如下。

代码:

    Public void ImageTest(String format, string path)
    {
        //Other Code

        //if-Else part
        try
        {
            if (strImageFormat.Equals("BMP"))
             {
                   if (Convert.ToString(dataRow["IsEmployee"]).ToUpper() == "TRUE")
                   {
                      ImagePath = string.Format("{0}{1}", fileNameUpper, ".BMP");
                   }
                   else
                    {
                      ImagePath = string.Format("{0}{1}", fileNamelabel, ".BMP");
                    }

             }      
            else if (strImageFormat.Equals("GIF"))
              {
                if (Convert.ToString(dataRow["IsEmployee"]).ToUpper() == "TRUE")
                   {
                    ImagePath = string.Format("{0}{1}", fileNameUpper, ".GIF");
                    }
                    else
                    {
                    ImagePath = string.Format("{0}{1}", fileNameUpper, ".GIF");
                    }
               }
            else if (strImageFormat.Equals("JPEG"))
              {
                if (Convert.ToString(dataRow["IsEmployee"]).ToUpper() == "TRUE")
                  {
                    ImagePath = string.Format("{0}{1}", fileNameUpper, ".JPEG");
                  }
                    else
                    {
                    ImagePath = string.Format("{0}{1}", fileNameUpper, ".JPEG");
                    }
                }
            else if (strImageFormat.Equals("PDF"))
                {
                  if (Convert.ToString(dataRow["IsEmployee"]).ToUpper() == "TRUE")
                    {
                     ImagePath = string.Format("{0}{1}", fileNameUpper, ".PDF");
                    }
                    else
                     {
                      ImagePath = string.Format("{0}{1}", fileNameUpper, ".PDF");
                      }
                 }
        }
        catch(Exception ex)
        {

        }
}

看起来代码

   if (Convert.ToString(dataRow["IsEmployee"]).ToUpper() == "TRUE")
   {
     ImagePath = string.Format("{0}{1}", fileNameUpper, ".GIF");
   }
   else
   {
     // fileNamelabel expected, not fileNameUpper
     ImagePath = string.Format("{0}{1}", fileNameUpper, ".GIF");
   }

要么是多余的,要么只是复制粘贴的。假设它是复制粘贴的:

  if (Convert.ToString(dataRow["IsEmployee"]).Equals("TRUE", StringComparison.OrdinalIgnoreCase))
    ImagePath = string.Format("{0}.{1}", fileNameUpper, strImageFormat);
  else
    ImagePath = string.Format("{0}.{1}", fileNamelabel, strImageFormat);  

注意 更改后的格式:{0}.{1}.

我宁愿不使用太多 switch 语句并将值存储在 bool 中,然后在 case 中使用条件运算符:

bool _condition = Convert.ToString(dataRow["IsEmployee"]);

switch(strImageFormat)
{
case "JPG":
            ImagePath = _condition  ? string.Format("{0}{1}", fileNameUpper, ".JPEG") : ImagePath = string.Format("{0}{1}", fileNamelabel, ".JPEG") ;
            break;
case "GIF":
            ImagePath = _condition  ? string.Format("{0}{1}", fileNameUpper, ".GIF") : ImagePath = string.Format("{0}{1}", fileNamelabel, ".GIF") ;
            break;
.
.
.
.
.
.
default:
       // DO SOMETHING
}

只是另一个想法,不需要 switch 语句。

bool isEmployee = Convert.ToString(dataRow["IsEmployee"]).ToUpper() == "TRUE";
ImagePath = string.Format("{0}.{1}", isEmployee ? fileNameUpper : fileNamelabel, strImageFormat);

我会在 C# 中使用工厂模式。这使您的代码更加灵活,并且由于字符串的开关无论如何都会在 C# 中转换为字典,因此它在性能方面并不重要。

有关实现的详细信息,我不久前在 上发布了一个实现。

我认为您不应该使用 switch case 而不是 ifs。

你应该以正确的方式解决它,这意味着使用多态性。

看看设计模式http://www.dofactory.com/net/factory-method-design-pattern

看看下面的初始骨架:

public static class TestEliminateSwitch
{
    public static string GetImagePath()
    {
        var formatFactory = new FormatFactory();
        var instance = formatFactory.GetFomatClass("PDF");
        return instance.GetImagePath("TRUE");
    }
}
public class FormatFactory
{
    public FormatBase GetFomatClass(string formatName)
    {
        string className = typeof (FormatBase).FullName.Replace("Base", formatName);
        return Assembly.GetExecutingAssembly()
            .CreateInstance(className) as FormatBase;
    }
}
public abstract class FormatBase
{
    public string fileNameUpper = string.Empty;
    public string fileNamelabel = string.Empty;
    public virtual string GetImagePath(string IsEmployee)
    {
        return string.Format("{0}{1}", IsEmployee.ToUpper() == "TRUE" ? fileNameUpper : fileNamelabel, GetFileExtention());
    }

    public abstract string GetFileExtention();
}
class FormatPDF : FormatBase
{
    public override string GetFileExtention()
    {
        return ".PDF";
    }
}
class FormatGIF : FormatBase
{
    public override string GetFileExtention()
    {
        return ".GIF";
    }
}