帮我优化这段C#代码 public double[] GetProfile(Beam beam) { double num = 0.0; double num1 = 0.0; double num2 = 0.0; double num3 = 0.0; double num4 = 0.0; string[] str = Regex.Replace(beam.Profile.ProfileString, "[a-z]", "", RegexOptions.IgnoreCase).Split('-', '*'); if (beam.Profile.ProfileString.StartsWith("HI") || beam.Profile.ProfileString.StartsWith("PHI") || beam.Profile.ProfileString.StartsWith("WI") || beam.Profile.ProfileString.StartsWith("CC") || beam.Profile.ProfileString.StartsWith("ZZ")) { switch (str.Length) { case 5: num = Convert.ToDouble(str[0]); num1 = Convert.ToDouble(str[2]); num2 = Convert.ToDouble(str[3]); num3 = Convert.ToDouble(str[4]); num4 = Convert.ToDouble(str[1]); break; case 4: num = Convert.ToDouble(str[0]); num1 = Convert.ToDouble(str[1]); num2 = Convert.ToDouble(str[2]); num3 = Convert.ToDouble(str[3]); num4 = Convert.ToDouble(str[0]); break; } } else { num = Convert.ToDouble(str[0]); num1 = Convert.ToDouble(str[2]); num2 = Convert.ToDouble(str[3]); num3 = Convert.ToDouble(str[1]); num4 = Convert.ToDouble(str[0]); } return new double[5] { num, num1, num2, num3, num4 }; }
时间: 2024-02-10 11:19:30 浏览: 79
C#生成设置范围内的Double类型随机数的方法
可以对这段代码进行如下优化:
1.将重复的变量定义合并为一个语句,如下:
double num = 0.0, num1 = 0.0, num2 = 0.0, num3 = 0.0, num4 = 0.0;
2.使用 var 关键字定义变量,可以减少代码量,如下:
var str = Regex.Replace(beam.Profile.ProfileString, "[a-z]", "", RegexOptions.IgnoreCase).Split('-', '*');
3.使用 switch-case 语句替换 if-else 语句,可以使代码更加清晰,如下:
switch (beam.Profile.ProfileString.Substring(0, 2))
{
case "HI":
case "PH":
case "WI":
case "CC":
case "ZZ":
switch (str.Length)
{
case 5:
num = Convert.ToDouble(str[0]);
num1 = Convert.ToDouble(str[2]);
num2 = Convert.ToDouble(str[3]);
num3 = Convert.ToDouble(str[4]);
num4 = Convert.ToDouble(str[1]);
break;
case 4:
num = Convert.ToDouble(str[0]);
num1 = Convert.ToDouble(str[1]);
num2 = Convert.ToDouble(str[2]);
num3 = Convert.ToDouble(str[3]);
num4 = Convert.ToDouble(str[0]);
break;
}
break;
default:
num = Convert.ToDouble(str[0]);
num1 = Convert.ToDouble(str[2]);
num2 = Convert.ToDouble(str[3]);
num3 = Convert.ToDouble(str[1]);
num4 = Convert.ToDouble(str[0]);
break;
}
4.使用数组初始化器来初始化数组,如下:
return new double[] { num, num1, num2, num3, num4 };
优化后的代码如下:
public double[] GetProfile(Beam beam)
{
double num = 0.0, num1 = 0.0, num2 = 0.0, num3 = 0.0, num4 = 0.0;
var str = Regex.Replace(beam.Profile.ProfileString, "[a-z]", "", RegexOptions.IgnoreCase).Split('-', '*');
switch (beam.Profile.ProfileString.Substring(0, 2))
{
case "HI":
case "PH":
case "WI":
case "CC":
case "ZZ":
switch (str.Length)
{
case 5:
num = Convert.ToDouble(str[0]);
num1 = Convert.ToDouble(str[2]);
num2 = Convert.ToDouble(str[3]);
num3 = Convert.ToDouble(str[4]);
num4 = Convert.ToDouble(str[1]);
break;
case 4:
num = Convert.ToDouble(str[0]);
num1 = Convert.ToDouble(str[1]);
num2 = Convert.ToDouble(str[2]);
num3 = Convert.ToDouble(str[3]);
num4 = Convert.ToDouble(str[0]);
break;
}
break;
default:
num = Convert.ToDouble(str[0]);
num1 = Convert.ToDouble(str[2]);
num2 = Convert.ToDouble(str[3]);
num3 = Convert.ToDouble(str[1]);
num4 = Convert.ToDouble(str[0]);
break;
}
return new double[] { num, num1, num2, num3, num4 };
}
阅读全文