C# is a typical object-orient programming language. It offers all the support you need to create your own class hierarchies and to use them in the most efficient way.
Interfaces are the key concept inside C#. An interface contains a public signature of Methods, Properties, Event, 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
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 precedes the specified object in the sort order.
- = 0 whch 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.
Ex
-
- class Car : IComparable
- {
- public string Name { get; set; }
- public int TopSpeed { get; set; }
-
-
- public int CompareTo(object obj)
- {
-
- Car car = (Car)obj;
-
-
- 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)
- {
-
- List<Car> cars = new List<Car>();
- cars.Add(new Car
- {
- TopSpeed = 200
- });
- cars.Add(new Car
- {
- TopSpeed = 180
- });
-
- cars.Sort();
- }
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.
ex
-
- class FileManager : IDisposable
- {
- FileStream fs;
-
- public FileManager(string filePath)
- {
- fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
- }
-
-
- public void Write(byte[] data)
- {
- fs.Write(data,0,data.Length);
- }
-
-
- public void Dispose()
- {
- fs.Close();
- }
-
- }
The code for using this method.
- static void Main(string[] args)
- {
-
- 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);
Ex
-
- class Person : IFormattable
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
-
-
- 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));
- Console.WriteLine(person.ToString("LF", null));
- Console.WriteLine(person.ToString("F", null));
- Console.WriteLine(person.ToString("L", null));
- }
Conclusion
Thses interfaces are the most common interfaces that you can use to create robust objects. There are also many interfaces such as -IEnumberable, IQuerable, IList<>, but the above three are very important.