Hi bengamin,
Here is your sample:
public class Person
: IComparable
{
private int age;
private string lastName;
private string firstName;
private bool maleGender;
public int CompareTo(object obj) {
Person secondPerson=obj as Person;
if( secondPerson==null ) {
throw new ArgumentNullException("obj");
}
int result;
result=(age-obj.age);
if( result!=0 ) {
return result;
}
result=lastName.CompareTo(obj.lastName);
if( result!=0 ) {
return result;
}
result=firstName.CompareTo(obj.firstName);
if( result!=0 ) {
return result;
}
result=maleGender.CompareTo(obj.maleGender);
return result;
}
}
Cheers!
Marcin
Thank you for your help!
Can you give us some example?
"Marcin Grzêbski" <
[email protected]> ????
Hi,
Good practise is to implement "IComparable" interface.
int CompareTo(Object obj)
- it should return zero if "obj" is equal to base object.
Then you have to (easily) override the "Equals()", as follow:
public override bool Equals(Object obj) {
return (this.CompareTo(obj)==0);
}
Cheers
Marcin
Hi,
I have a C# class and two instance of the class;
the class have some property.
I want to compare the property value of the two instance
How should i do? override == ? use delegate ?
I am sorry ,my english was poor.