优化这段代码 string extension = ".pdf";//扩展名 “.aspx” int transcodeType = 0; if (extension.Contains(".ppt,.pptx,.pdf,.doc,.docx")) { transcodeType = 1; }else if (extension.Contains(".mp4,.3pg,.mpeg,.avi,.flv,.wmv,.rm,.h264")) { transcodeType = 2; }else if (extension.Contains(".mp3,.wav,.wma,.aac,.flac,opus")) { transcodeType = 2; }
时间: 2023-05-31 16:07:15 浏览: 741
要优化这段代码,可以将多个扩展名放进同一个 Contains 方法中,以简化代码。同时,可以使用 switch 语句代替多个 if-else 分支结构,使代码更易读更高效。
优化后的代码如下:
string extension = ".pdf";
int transcodeType = 0;
switch (extension)
{
case ".ppt":
case ".pptx":
case ".pdf":
case ".doc":
case ".docx":
transcodeType = 1;
break;
case ".mp4":
case ".3pg":
case ".mpeg":
case ".avi":
case ".flv":
case ".wmv":
case ".rm":
case ".h264":
transcodeType = 2;
break;
case ".mp3":
case ".wav":
case ".wma":
case ".aac":
case ".flac":
case "opus":
transcodeType = 3;
break;
default:
//处理无法识别的扩展名
break;
}
阅读全文