1.Equals & IsNullOrEmpty

要求—-想要对Point类写一个类似string.IsNullOfEmpty的函数检查经纬度是否为0即Empty

  1. public class Point
  2. {
  3. public double Lat{get;set;}
  4. public double Lon{get;set;}
  5. public string Tag{get;set;}
  6. public Person(double lon, double lat)
  7. {
  8. this.Lat = lat;
  9. this.Lon = lon;
  10. }
  11. public Point(double lon,double lat,string tag)
  12. {
  13. this.Lon = lon;
  14. this.Lat = lat;
  15. this.Tag = tag;
  16. }
  17. }

想到了重写Equals的步骤—-在VS中写Equals并Tab两次则会出现完整的Equals函数

  1. // override object.Equals
  2. public override bool Equals(object obj)
  3. {
  4. if (obj == null)
  5. {
  6. return false;
  7. }
  8. if ((obj.GetType().Equals(this.GetType())) == false)
  9. {
  10. return false;
  11. }
  12. Point point = null;
  13. point = (Point)obj;
  14. return this.Lat.Equals(point.Lat) && this.Lon.Equals(point.Lon);
  15. }
  16. // override object.GetHashCode
  17. public override int GetHashCode()
  18. {
  19. return this.Lat.GetHashCode() + this.Lon.GetHashCode();
  20. }

然后再写IsNullOrEmpty函数

  1. public static bool IsNullOrEmpty(Point p)
  2. {
  3. if (p == null)
  4. {
  5. return true;
  6. }
  7. else if (p.Equals(new Point(0, 0)))
  8. {
  9. return true;
  10. }
  11. else
  12. {
  13. return false;
  14. }
  15. }

2.Sort