//additonal public static void UseofComparable() { CricketPlayers SachinObj = new CricketPlayers { Name = "Sachin", Age = 30, Runs = 2000 }; CricketPlayers DhoniObj = new CricketPlayers { Name = "Dhoni", Age = 29, Runs = 1200 }; //Compare Sachinobj with DhoniObj int state = SachinObj.CompareTo(DhoniObj); if (state == 1) { Console.WriteLine("Sachins runs - {0} has scored runs more than Dhonis Runs -{1} ", SachinObj.Runs, DhoniObj.Runs); } if (state == -1) { Console.WriteLine("Sachins runs - {0} has scored runs less than Dhonis Runs- {1} ", SachinObj.Runs, DhoniObj.Runs); } if (state == 0) { Console.WriteLine("Sachins runs - {0} and Dhonis Runs-{1} are equal", SachinObj.Runs, DhoniObj.Runs); } }
public int CompareTo(object obj) { CricketPlayers objStudent = (CricketPlayers)obj; if (this.Runs > objStudent.Runs) { return(1); } if (this.Runs < objStudent.Runs) { return(-1); } else { return(0); } }