I created a small test program to gain some experience with the IComparable interface in C#. I need to use the interface for other stuff in future.
In the program I'd like to sort a ArraList of customers based on the salary. Executing my code I always receive the "System.InvalidOperationException" exception.
I already tried different versions of the code and checked a few tutorials, always with the same result.
My code:
using System;
using System.Collections;
namespace CsTest
{
public class Program
{
public static void Main()
{
Customer customer1 = new Customer()
{
ID = 101,
Name = "Ted",
Salary = 4000
};
Customer customer2 = new Customer()
{
ID = 102,
Name = "Tod",
Salary = 7000
};
Customer customer3 = new Customer()
{
ID = 103,
Name = "Tom",
Salary = 5500
};
ArrayList listCustomers = new ArrayList();
listCustomers.Add(customer1);
listCustomers.Add(customer2);
listCustomers.Add(customer3);
Console.WriteLine("Customers before sorting");
foreach (Customer customer in listCustomers)
{
Console.WriteLine(customer.Name + " - " + customer.Salary);
}
// Sort() method should sort customers by salary
listCustomers.Sort();
Console.WriteLine("Customers after sorting");
foreach (Customer customer in listCustomers)
{
Console.WriteLine(customer.Name + " - " + customer.Salary);
}
}
}
public class Customer : IComparable<Customer>
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public int CompareTo(Customer obj)
{
return this.Salary.CompareTo(obj.Salary);
}
}
}