Question Need an explanation

mickey donny

New member
Joined
Oct 3, 2018
Messages
3
Programming Experience
1-3
Hello guys,

I have a project to do for school in C# and this is part of my code. The code works fine, but if you guys could give me tips on how it should improved, that would be awesome. I have a hard time understanding why new Cours(search) works to find a certain object with the same name. The Cours class constructor takes a string parameter for the name attribute and implements the IComparable interface.

Here is some code :
C#:
private List<Cours> cours;

...

public Cours Search(string search){
      cours.Sort();
      int index = Array.BinarySearch(cours.ToArray(), new Cours(search));
      return cours[index];
}

Thank you

- Mickey
 
The IComparable interface builds into a type the ability to compare two instances and determine their relative magnitude. If you have instance A and you call its CompareTo method and pass instance B as an argument, you will get back an 'int' value that indicates which is "less" and which is "greater" than the other. The possible values returned are generally -1, 0 and 1 but it can, in principle, be any 'int' value. A value less than zero indicates that instance A is "less" than instance B, i.e. A would come before B when sorting in ascending order. A value greater than zero indicates that instance A is "greater" than instance B, i.e. A would come after B when sorting in ascending order. A value of zero indicates that the two instances are equivalent. That Array.BinarySearch method will search the array you specify for an element that, when compared to the object you provide, returns a value of zero from its CompareTo method. The CompareTo method of your Cours class is presumably implemented such that the comparison is done on Name, e.g.
public class Cours : IComparable
{
    public string Name { get; set; }
    public int CompareTo(object obj)
    {
        return Name.CompareTo(((Cours) obj).Name);
    }
}

There are two things that I would change, although one you may already have done. Firstly, it is good practice to always implement both IComparable and IComparable<T>:
public class Cours : IComparable, IComparable<Cours>
{
    public string Name { get; set; }
    public int CompareTo(object obj)
    {
        return CompareTo((Cours) obj);
    }

    public int CompareTo(Cours other)
    {
        return Name.CompareTo(other.Name);
    }
}

That one you may already have done but you did mention IComparable specifically so maybe not.

The other change is to not sort the List in that Search method. It may not be an issue but that method shouldn't really change the List in the course of sorting. Rather than sorting first and converting to an array, I would convert to an array first and then sort. That means that you're sorting the array and leaving the List in the same state as you found it:
public Cours Search(string search){
      var arr = cours.ToArray();

      Array.Sort(arr);
      int index = Array.BinarySearch(arr, new Cours(search));
      return arr[index];
}

You might even choose to sort as part of the conversion:
public Cours Search(string search){
      var arr = cours.OrderBy(c => c).ToArray();

      int index = Array.BinarySearch(arr, new Cours(search));
      return arr[index];
}

Actually, it occurs to me that a third change that I'd make is to change the name of that method parameter too. 'search' is a bad name. You're search by Name so the parameter should reflect that, i.e. I'd call the parameter 'name':
public Cours Search(string name){
 
Back
Top Bottom