不用while、for写死循环。

本文通过一个具体的代码示例介绍了如何在C#中因为操作符重载不当而导致死循环的问题,并给出了修正建议。
  第一次在博客园开博,所以一定要置顶。
  今天终于又写了一死循环程序。陷入了操作符重载这个陷阱。
  代码如下:
None.gifpublic class Line : ICloneable
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private Point2D endPoint;
InBlock.gif        
private Point2D startPoint;
InBlock.gif
InBlock.gif        
private Line()
InBlock.gif            : 
this(Point2D.Empty, Point2D.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Line(Point2D startPoint, Point2D endPoint)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.startPoint = startPoint;
InBlock.gif            
this.endPoint = endPoint;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public object Clone()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new Line(this.startPoint, this.endPoint);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override bool Equals(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (obj != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!(obj is Line))               
InBlock.gif                    
return false;
InBlock.gif               
InBlock.gif                Line line 
= (Line)obj;
InBlock.gif                
if (this.startPoint.Equals(line.startPoint))
InBlock.gif                    
return this.endPoint.Equals(line.endPoint);                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool operator ==(Line line1, Line line2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (line1 == null && line2 == null)     
InBlock.gif                
return true;
InBlock.gif            
if (line1 != null && line2 != null)
InBlock.gif                
return line1.Equals(line2);
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool operator !=(Line line1, Line line2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (line1== null && line2==null)
InBlock.gif                
return false;
InBlock.gif            
if (line1 != null && line2 != null)
InBlock.gif                
return !line1.Equals(line2);
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public static Line Empty
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new Line(Point2D.Empty, Point2D.Empty);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Browsable(
false)]
InBlock.gif        
public bool IsEmpty
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return startPoint.IsEmpty ? endPoint.IsEmpty : false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
  这个一个描述一根线条的类,这段代码怎么编译都不会出错。但请大家留意中间的这段代码:
None.gif
None.gif        
public static bool operator ==(Line line1, Line line2)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if (line1 == null && line2 == null)     
InBlock.gif                
return true;
InBlock.gif            
if (line1 != null && line2 != null)
InBlock.gif                
return line1.Equals(line2);
InBlock.gif            
return false;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
public static bool operator !=(Line line1, Line line2)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if (line1== null && line2==null)
InBlock.gif                
return false;
InBlock.gif            
if (line1 != null && line2 != null)
InBlock.gif                
return !line1.Equals(line2);
InBlock.gif            
return true;
ExpandedBlockEnd.gif        }
  这就是个不折不扣的死循环,而且是死得很惨的那种。
  如果你在某个地方使用了如下形式的代码,不死循环都很难。
None.gif Line line1 = new Line();
None.gif            Line line2 
= new Line();
None.gif            
if (line1==line2)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{               
ExpandedBlockEnd.gif            }
  
  要想解决上面的问题,必须把代码改成如下形式:       
None.gifpublic static bool operator ==(Line line1, Line line2)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if (object.Equals(line1, null&& object.Equals(line2, null))           
InBlock.gif                
return true;           
InBlock.gif            
if (!object.Equals(line1, null&& !object.Equals(line2, null))            
InBlock.gif                
return line1.Equals(line2);           
InBlock.gif            
return false;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
public static bool operator !=(Line line1, Line line2)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if (object.Equals(line1, null&& object.Equals(line2, null))           
InBlock.gif                
return false;           
InBlock.gif            
if (!object.Equals(line1, null&& !object.Equals(line2, null))            
InBlock.gif                
return !line1.Equals(line2);           
InBlock.gif            
return true;
ExpandedBlockEnd.gif        }
  不过这里line是一个class,如果是struct,不知会这么样。还没试过呢。

转载于:https://www.cnblogs.com/xlq/archive/2007/04/30/733373.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值