Introduction
C# is a typical object-orient programming language. It offers all the support you need to create your own class hierarchies and use them in the most efficient way.
Interfaces are the key concept inside C#. An interface contains a public signature of Methods, Properties, Events, and Indexes. A class or a structure can implement an interface.
In this article, I will show most of the popular built-in interfaces that you can use to enhance your class hierarchies.
IComparable interface
It contains one method that you can use to sort the elements.
int CompareTo(object obj);
The CompareTo method returns an int value that shows how two elements are related to each other. The possible values are.
- < 0 which means the current instances precede the specified object in the sort order.
- = 0, which means that the current instance is in the same position of a specified object in the sort order.
- > 0 which means that the current instance follows the specified object in the sort order.
Example
class Car : IComparable
{
public string Name { get; set; }
public int TopSpeed { get; set; }
// The implementation method
public int CompareTo(object obj)
{
// Convert the specified object to a car
Car car = (Car)obj;
// Compare between the current instance and the specified object
if (this.TopSpeed > car.TopSpeed)
return 1;
else if (this.TopSpeed == car.TopSpeed)
return 0;
else
return -1;
}
}
The code for using this method.
static void Main(string[] args)
{
// Using these objects
List<Car> cars = new List<Car>();
cars.Add(new Car
{
TopSpeed = 200
});
cars.Add(new Car
{
TopSpeed = 180
});
cars.Sort(); // This method will call the CompareTo method in each object to sort the objects
}
IDisposable interface
This interface is very powerful if your class works with unmanaged resources (files, streams, database connections, etc..). This interface contains one method.
void Dispose();
When you implement this method, you can free any unmanaged and external resources that you’ve used in your class instance.
class FileManager : IDisposable
{
FileStream fs;
public FileManager(string filePath)
{
fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
}
// Method to write
public void Write(byte[] data)
{
fs.Write(data, 0, data.Length);
}
// The implementation method to free any unmanaged resources
public void Dispose()
{
fs.Close();
}
}
The code for using this method.
static void Main(string[] args)
{
// Using statement will call the Dispose Method after the statement is finished
using (FileManager manager = new FileManager("welcome.data"))
{
byte[] data = Encoding.ASCII.GetBytes("Hello C#");
manager.Write(data);
}
}
IFormattable interface
The IFormattable interface converts any object to its representation string depending on the specific format provider, and it contains only one method.
string ToString(string format, IFormatProvider provider);
class Person : IFormattable
{
public string FirstName { get; set; }
public string LastName { get; set; }
// This method to convert the object to string depends on a specific format and culture
public string ToString(string format, IFormatProvider formatProvider)
{
if (formatProvider == null)
formatProvider = System.Globalization.CultureInfo.CurrentCulture;
switch (format)
{
case "FL":
return FirstName + " " + LastName;
case "LF":
return LastName + " " + FirstName;
case "F":
return FirstName;
case "L":
return LastName;
default:
throw new ArgumentException("This format is not provided");
}
}
}
The code for using this method.
static void Main(string[] args)
{
Person person = new Person
{
FirstName = "Ahmad",
LastName = "Mozaffar"
};
Console.WriteLine(person.ToString("FL", null)); // Ahmad Mozaffar
Console.WriteLine(person.ToString("LF", null)); // Mozaffar Ahmad
Console.WriteLine(person.ToString("F", null)); // Ahmad
Console.WriteLine(person.ToString("L", null)); // Mozaffar
}
Conclusion
These interfaces are the most common interfaces that you can use to create robust objects. There are also many interfaces, such as -IEnumberable, IQuerable, and IList<>, but the above three are very important.