Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多if-else的switch语句的最佳方式_C#_Switch Statement - Fatal编程技术网

C# 多if-else的switch语句的最佳方式

C# 多if-else的switch语句的最佳方式,c#,switch-statement,C#,Switch Statement,我正试图把很多“如果还有”转换成“交换”状态 需要指针进行最佳切换的情况下,一些代码结构如下 代码: 看起来代码 if (Convert.ToString(dataRow["IsEmployee"]).ToUpper() == "TRUE") { ImagePath = string.Format("{0}{1}", fileNameUpper, ".GIF"); } else { // fileNamelabel expected, not f

我正试图把很多“如果还有”转换成“交换”状态 需要指针进行最佳切换的情况下,一些代码结构如下

代码:

看起来代码

   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

您应该以正确的方式解决它,即使用多态性

看看设计模式

查看以下初始骨架:

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";
    }
}

谢谢Soner的编辑
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";
    }
}