0

鉴于下面的简单代码块,我想知道是否有更好的方法在 C# 中进行编码

        int lowIndex = 0;
        int highIndex = 1;
        if (end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres())
        {
            if (end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMillimetres())
            {
                lowIndex = 1;
                highIndex = 0;
            }
        }
        else
        {
            if (end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres())
            {
                lowIndex = 1;
                highIndex = 0;
            }
        }
4

7 回答 7

4

像这样的东西怎么样

int lowIndex = 0; 
int highIndex = 1; 
if ((end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() && end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMillimetres()) ||
    (end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres()))
{ 
    lowIndex = 1; 
    highIndex = 0; 
} 
于 2012-09-12T05:33:02.150 回答
1

也许是这样的:

int X0mm = end[0].X.ConvertToMillimetres();
int X1mm = end[1].X.ConvertToMillimetres();
int Y0mm = end[0].Y.ConvertToMillimetres();
int Y1mm = end[1].Y.ConvertToMillimetres();

int lowIndex = (X0mm == X1mm && Y0mm > Y1mm) || (X0mm > X1mm) ? 1 : 0;
int highIndex = lowIndex == 1 ? 0 :1;
于 2012-09-12T05:34:39.233 回答
1

我认为您要做的是消除设置两次的两条lowIndex线highIndex。您可以IF像这样组合语句。

int lowIndex = 0;
int highIndex = 1;
if ( (end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() &&
      end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMillimetres()) ||
      end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres() )
{
    lowIndex = 1;
    highIndex = 0;
}
于 2012-09-12T05:36:55.683 回答
1

当然:

int lowIndex = 0;
int highIndex = 1;
if (   end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() 
    && end[0].Y.ConvertToMillimetres() >  end[1].Y.ConvertToMillimetres()  
    || end[0].X.ConvertToMillimetres() != end[1].X.ConvertToMillimetres()
    && end[0].X.ConvertToMillimetres() >  end[1].X.ConvertToMillimetres())
{
    lowIndex = 1;
    highIndex = 0;
}

end[0].X.ConvertToMillimetres() != end[1].X.ConvertToMillimetres() && end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres()将始终等价于end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres(),因此:

int lowIndex = 0;
int highIndex = 1;
if (   end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() 
    && end[0].Y.ConvertToMillimetres() >  end[1].Y.ConvertToMillimetres()  
    || end[0].X.ConvertToMillimetres() >  end[1].X.ConvertToMillimetres())
{
    lowIndex = 1;
    highIndex = 0;
}

最后,我不确定 ConvertToMillimetres 的结果是什么或它有多复杂/如果 ConvertToMillimetres 使用一些局部变量来捕获这些方法的值以减少计算需要大量时间......不是为了节省一点时间而污染您的本地范围可能不值得。很可能,这是一个相当微不足道的功能,所以它不会很有优势。(end[0] 和 end 1作为局部变量可能会更好,正如 Krishna 所说的那样。或者甚至 end 1 .X 和 end 1 .Y 等。但如果你这样做,不妨保存结果。)

//capture values

var end0Xm = end[0].X.ConvertToMillimetres();
var end1Xm = end[1].X.ConvertToMillimetres();
var end0Ym = end[0].Y.ConvertToMillimetres();
var end1Ym = end[1].Y.ConvertToMillimetres();

//define proper lowIndex, highIndex
int lowIndex = 0;
int highIndex = 1;
if (   end0Xm  == end1Xm 
    && end0Ym  >  end1Ym  
    || end0Xm  >  end1Xm )
{
    lowIndex = 1;
    highIndex = 0;
}

保存测试结果以供将来使用可能会很有用,这也消除了 if 块,这样可以减少将来有人搞砸的机会。但是,您仍然必须有条件地做一些事情。下一个代码块假设您知道并理解C# 的三元运算符

var end0Xm = end[0].X.ConvertToMillimetres();
var end1Xm = end[1].X.ConvertToMillimetres();
var end0Ym = end[0].Y.ConvertToMillimetres();
var end1Ym = end[1].Y.ConvertToMillimetres();

//define proper lowIndex, highIndex
bool testCase = (end0Xm  == end1Xm 
    && end0Ym  >  end1Ym  
    || end0Xm  >  end1Xm);

int lowIndex = testCase? 1 : 0;
int highIndex = testCase? 0 : 1; 

或者,也许你更喜欢highIndex = !testcase? 1: 0,甚至highIndex = 1 - lowIndex

等等等等。

于 2012-09-12T05:53:28.037 回答
1

我更喜欢可读性而不是紧凑的代码!:) 重命名变量,因为它最适合您的代码...

int xComparison = end[0].X.ConvertToMillimetres().CompareTo(end[1].X.ConvertToMillimetres());
int yComparison = end[0].Y.ConvertToMillimetres().CompareTo(end[1].Y.ConvertToMillimetres());

bool isMatch = ((xComparison == 0 && yComparison > 0) || xComparison > 0);

int lowIndex = (isMatch ? 1 : 0);
int highIndex = (isMatch ? 0 : 1);
于 2012-09-12T08:13:45.707 回答
0

不要认为这是 C# 4.0 特定的,但您可以使其更具可读性:

var endOne = end[0];
var endTwo = end[1];

//now, if you would override the == operator for the type of X and Y to compare using ConvertToMillimetres(), you can have something like:

int lowIndex = (endOne.X == endTwo.X && endOne.Y > endTwo.Y) || (endOne.X > endTwo.X) ? 1 : 0;
int highIndex = lowIndex == 1 ? 0 : 1;
于 2012-09-12T05:39:04.903 回答
0

我会做一个方法:(代码可维护)

private void GetValueM(List<EndType> end,out int  lowIndex,out int  highIndex)
    {
         lowIndex = 0;
         highIndex = 1;

         if ((end != null) && (end.Count > 2))
         {
             var x0 = end[0].X;
             var x1 = end[1].X;
             var y0 = end[0].Y;
             var y1 = end[1].Y;

             if (x0 != null && x1 != null && y0 != null && y1 != null)
             {
                 if ((x0.ConvertToMillimetres() == x1.ConvertToMillimetres() && y0.ConvertToMillimetres() > y1.ConvertToMillimetres()) ||
                     (x0.ConvertToMillimetres() > x1.ConvertToMillimetres()))
                 {
                     lowIndex = 1;
                     highIndex = 0;
                 }

             }
             else
             {
                 //Any is null  set your value or throw exception
             }
         }


    }
于 2012-09-12T06:02:28.970 回答