添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
玩篮球的黄豆  ·  Unable to draw ...·  1 月前    · 
俊秀的毛衣  ·  Primitive - OpenGL Wiki·  1 月前    · 
飘逸的萝卜  ·  Structurally compare ...·  7 月前    · 
爽快的手链  ·  geometry - How do CSS ...·  12 月前    · 
独立的土豆  ·  欧盟中国商会·  2 月前    · 
自信的小狗  ·  Eagle 7 配备 ...·  3 月前    · 
  • Task Parallel Library
  • Back to basics: algorithms, data structures, interview questions
  • Emailing
  • Globalization
  • Generics
  • Reflection
  • Diagnostics
  • String operations
  • File I/O
  • Other technologies
  • Node.js
  • Amazon cloud/Big Data
  • Python
  • JavaScript
  • GitHub
  • About
  • Contact
  • In this post we saw how to determine if two arrays are structurally equal in .NET. Two arrays are said to be structurally equal if they contain the same elements in the same order.

    Structural equality has a comparison counterpart: IStructuralComparable. It determines if an array comes before or after or is equal to another array based on the elements within it.

    The Compare method returns an integer that follows the conventions of IComparer.Compare. We’ll need to override the non-generic IComparer interface.

    Let’s reuse the Triangle object from the IStructuralEquatable post with a method to calculate its area:

    public class Triangle public double BaseSide { get; set; } public double Height { get; set; } public double GetArea() return (BaseSide * Height) / 2;

    We’ll compare two triangles based on their areas so we’ll implement the following comparer:

    public class TriagleSizeComparer : IComparer private int Compare(Triangle x, Triangle y) return x.GetArea().CompareTo(y.GetArea()); public int Compare(object x, object y) return Compare((Triangle)x, (Triangle)y);

    We have two following two Triangle lists:

    List triangleListOne = new List() new Triangle(){BaseSide = 2, Height = 4}, new Triangle(){BaseSide = 4, Height = 5}, new Triangle(){BaseSide = 5, Height = 9}, new Triangle(){BaseSide = 3, Height = 8} List triangleListTwo = new List() new Triangle(){BaseSide = 2, Height = 4}, new Triangle(){BaseSide = 4, Height = 5}, new Triangle(){BaseSide = 5, Height = 9}, new Triangle(){BaseSide = 3, Height = 8}

    Just like in the case of IStructuralEquatable we need to explicitly convert the list to an array and declare it as an interface type:

    IStructuralComparable comparableArrayBase = triangleListOne.ToArray(); int compare = comparableArrayBase.CompareTo(triangleListTwo.ToArray(), new TriagleSizeComparer());

    “compare” results in 0 as the two arrays are equal. You can change the base and height properties of any triangle in the array and you’ll see the following results:

  • “compare” will be 1 is triangleListOne comes first in the order
  • “compare” will be -1 if triangleListTwo comes first instead
  • View all various C# language feature related posts here .

    Enter your email address to follow this blog and receive notifications of new posts by email.

    Email Address:

    History

    Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
    To find out more, including how to control cookies, see here: Cookie Policy