c# - What interface to implement to compare Arrays -
this question has answer here:
- easiest way compare arrays in c# 11 answers
i have custom class contains string. program has array of these classes , want able compare 1 array of these another.
i have implemented icomparable interface described here: http://www.informit.com/articles/article.aspx?p=25352&seqnum=4 find not allow me compare 1 array against another. (it allows me sort array)
public class myclass : icomparable<myclass> { #region private private string _name; #endregion #region public [xmlelement("name")] public string name { get{return _name;} set{_name= value;} } #endregion public int compareto(myclass class) { string s1 = this.name; string s2 = class.name; return string.compare(s1, s2); } override public string tostring() { return _name; } }
my program has array of these classes so:
myclass class1 = new myclass(); class1.name = "a"; myclass class2 = new myclass(); class2.name = "b"; myclass class3 = new myclass(); class3.name = "c"; myclass class4 = new myclass(); class4.name = "d"; myclass[] classes1 = { class1, class2, class3} myclass[] classes2 = { class1, class2, class4};
i can sort class if need be:
array.sort(classes2);
what interface need implement allow me compare 1 array against see if equal?
edit class custom , not inherit interfaces want know interface implement, how implement , how use it. example uses objects not custom: easiest way compare arrays in c#
your class needs override equals method.
alternatively, can implement iequatable interface.
can apply "sequenceequals" linq operator arrays in order achieve desire.
Comments
Post a Comment