sashomaga

Override Equals() ToString() GetHashCode() operators == !=

Mar 11th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.08 KB | None | 0 0
  1. enum specialties
  2. {
  3.     Informatic,HR,QualityAssurance
  4. }
  5.  
  6. public enum universities
  7. {
  8.     SofiaUniversity,Telerik
  9. }
  10.  
  11. public enum faculties
  12. {
  13.     RocketScience,AlienHunting
  14. }
  15.  
  16. class Student : ICloneable
  17. {
  18.  
  19.     #region Properties
  20.     public string FisrtName { get; private set;}
  21.     public string MidName { get; private set; }
  22.     public string LastName { get; private set; }
  23.     public int SSN { get; private set; }
  24.     public string MobilePfone { get; private set; }
  25.     public string Course { get; private set; }
  26.     public specialties Specialty { get; private set; }
  27.     public universities University { get; private set; }
  28.     public faculties Faculty { get; private set; }
  29.     #endregion
  30.  
  31.     #region Methods
  32.     public override bool Equals(object obj)
  33.     {
  34.         Student student = obj as Student;
  35.  
  36.         if (student == null)
  37.         {
  38.             return false;
  39.         }
  40.         //compare reference types
  41.         if (!object.Equals(this.FisrtName, student.FisrtName))
  42.         {
  43.             return false;
  44.         }
  45.         if (!object.Equals(this.MidName, student.MidName))
  46.         {
  47.             return false;
  48.         }
  49.         if (!object.Equals(this.LastName, student.LastName))
  50.         {
  51.             return false;
  52.         }
  53.         //compare value types
  54.         if (this.SSN != student.SSN)
  55.         {
  56.             return false;
  57.         }
  58.  
  59.         return true;
  60.     }
  61.            
  62.     public override int GetHashCode()
  63.     {
  64.         return this.FisrtName.GetHashCode() ^ this.SSN.GetHashCode();
  65.     }
  66.     public override string ToString()
  67.     {
  68.         return string.Format("First name: {0}, midle name: {1}, last name: {2}, SSN: {3}, mobile pfone: {4}, course: {5}, specialty: {6}, university: {7}, faculty: {8}",
  69.             this.FisrtName,this.MidName,this.LastName, this.SSN,this.MobilePfone,this.Course,this.Specialty,this.University,this.Faculty);
  70.     }
  71.     //operators
  72.     public static bool operator ==(Student st1, Student st2)
  73.     {
  74.         return object.Equals(st1, st2);
  75.     }
  76.     public static bool operator !=(Student st1, Student st2)
  77.     {
  78.         return !object.Equals(st1, st2);
  79.     }
  80.     //deep clone
  81.     public object Clone()
  82.     {
  83.         return new Student(FisrtName = this.FisrtName,MidName= this.MidName,LastName = this.LastName,SSN = this.SSN, MobilePfone = this.MobilePfone,Course = this.Course,
  84.         Specialty = this.Specialty, University = this.University, Faculty = this.Faculty);
  85.     }
  86.     //CompareTo()
  87.     public int CompareTo(Student student)
  88.     {
  89.         if (this.FisrtName != student.FisrtName)
  90.         {
  91.             return (this.FisrtName.CompareTo(student.FisrtName));
  92.         }
  93.         if (this.MidName != student.MidName)
  94.         {
  95.             return (this.MidName.CompareTo(student.MidName));
  96.         }
  97.         if (this.LastName != student.LastName)
  98.         {
  99.             return (this.LastName.CompareTo(student.LastName));
  100.         }
  101.         if (this.SSN != student.SSN)
  102.         {
  103.             return (this.SSN - student.SSN);
  104.         }
  105.         return 0;
  106.     }
  107.     //compare
  108.     public static List<Student> Compare(List<Student> studentList)
  109.     {
  110.         return studentList.OrderBy(x => x.FisrtName).ThenBy(x => x.MidName).ThenBy(x => x.LastName).ThenBy(x => x.SSN).ToList();
  111.     }
  112.     #endregion
  113.  
  114.     #region Constructors
  115.     public Student(string fName, string mName, string lName, int ssn, string mobilePfone, string course, specialties specialty, universities university, faculties faculty)
  116.     {
  117.         this.FisrtName = fName;
  118.         this.MidName = mName;
  119.         this.LastName = lName;
  120.         this.SSN = ssn;
  121.         this.MobilePfone = mobilePfone;
  122.         this.Course = course;
  123.         this.Specialty = specialty;
  124.         this.University = university;
  125.         this.Faculty = faculty;
  126.     }
  127.     #endregion
  128. }
  129.  
  130. public interface ICloneable
  131. {
  132.     object Clone();
  133. }
  134.  
  135. public interface IComparable
  136. {
  137.     void Compare();
  138. }
  139.  
  140. static void Main()
  141.     {
  142.         Student st1 = new Student(null,"Mishev","Petrov",100,"0888345678", "2a", specialties.HR , universities.SofiaUniversity , faculties.RocketScience);
  143.         Student st2 = new Student(null, "Mishev", "Petrov", 100, "0888345678", "2a", specialties.HR, universities.SofiaUniversity, faculties.RocketScience);
  144.         Student st3 = new Student("Mahmud", "Mishev", "Petrov", 999, "0888345678", "4a", specialties.HR, universities.SofiaUniversity, faculties.RocketScience);
  145.         //test equals
  146.         Console.WriteLine(st1.Equals(st2));
  147.         Console.WriteLine(st1.Equals(st3));
  148.         //test tostring
  149.         Console.WriteLine(st1.ToString());
  150.         //test operators
  151.         Console.WriteLine(st1 == st2);
  152.         Console.WriteLine(st1 != st2);
  153.         //test get hash code
  154.         Console.WriteLine(st3.GetHashCode());
  155.         //test clone
  156.         Student cloned = st1.Clone() as Student;
  157.         //test CompareTo
  158.         Console.WriteLine(st1.CompareTo(st2));
  159.         //test compare
  160.         List<Student> studentList = new List<Student>(new Student[] {st1, st2, st3});
  161.         var sorted = Student.Compare(studentList);      
  162.     }
Advertisement
Add Comment
Please, Sign In to add comment