In the previous lecture, we saw how we can add a constraint to a generic if we need a data type to implement a specific interface. In this lecture, we'll learn how to implement an interface. In this example, we want to create an ordered dynamic array of rectangles, and then add rectangles to that array, and then print the rectangles in the array. So, here's our Rectangle class at this point. We just have width and height fields, a constructor, properties to get the width and height, and a ToString method so we can convert the rectangle to a string to print it out in our main method. So, the first thing I want to do is create an ordered dynamic array of rectangle, and I'll just call it rectangles. And of course, I'll create a new one, and I'll hit Enter so you can see it all on the screen. So, when I compile, I get an error. And the error says that the Rectangle class doesn't implement the IComparable interface. And we know that because we didn't do anything to implement that interface. So, over here in the Rectangle class, the first thing we do is we say that this class will implement the IComparable interface. And you'll notice that the syntax here is just like when we say a particular class is a child class of another class. But in this case, we're just saying, we will provide whatever methods the IComparable interface requires. And if you remember from our discussion about the ordered dynamic array, the method that we need is the CompareTo method. If I try to compile again, I get another error. And this error says that the Rectangle class doesn't implement the CompareTo method. So, once I say, I'm going to implement a particular interface, I'm required to provide all the methods in that interface. How do you know what those methods are? Well, you read the documentation. Here's the documentation for the IComparable interface. And you'll notice that it says that it defines a type-specific comparison that the class implements to order or sort its instances, and that's exactly what we need. And if we scroll down, we find out there's a specific method and that's all we need. And if we click on that method, we can get more details about how that method is supposed to work. Specifically, the CompareTo method compares this object to another object of the same type, and returns information about the relative order. So, if the current instance is before the parameter then we return a negative number, and if they're at the same position in terms of order then we return zero, and if it's after the parameter then we return some number greater than zero. If, in fact, the parameter is not the same data-type as the class in which we're implementing this method, we actually throw an argument exception. And there is an example right here. And the easiest thing to do, is just to copy this and bring it over into our rectangle code, and then make the appropriate adjustments. So, first of all, I'll add a documentation comment. And it returns relative order. And this is the object to compare to. I'll do some adjusting of curly braces and add some comments as well. So, this is always greater than null. And that by definition in the documentation, you are allowed to omit the curly braces for an if statement if you only have one statement, but I always include curly braces, so I'll do that now. We'll also make sure that the object is actually a rectangle. So, I'll say, check object type. And of course, this is a rectangle. And remember, this is like a type cast, so if we say, obj as Rectangle, if this is successful, if in fact, obj is a Rectangle, I'll rename this to be otherRectangle instead. So, if this is successful because obj is a rectangle, then we can now treat other rectangle as a rectangle object. If obj is not a rectangle, then this will be null at this point, and that's why we have this check for null. And if the object is not a rectangle, so obj isn't a rectangle, then we throw the exception because that's what the documentation tells us we should do. If, in fact, we actually have a rectangle here, we now need to decide how do we tell which one comes before the other in the sort order. And I'm going to say that we're going to sort by area. So, if width times height, so this is the width and height of the rectangle we're inside right now, if that is less than the other rectangle width times the other rectangle height, then that means that this rectangle comes before the other one in the sort order, so I'll return negative one. Remember, it just has to be any number less than zero, but I typically return negative negative one, zero, and one from my CompareTo method. We now need to check to see if they're equal. So, if they're equal, I will return zero. And the only other possibility is that the current rectangle we're inside comes after the other one. And I don't have to check anything at this point, I can just return one. So, we knew, when we implemented the IComparable interface, that we had to provide a CompareTo method, and we read the documentation for the CompareTo method to see exactly how that method had to behave. I can now compile just fine. So, this is very exciting that we've finally been able to create the array of rectangles. The big idea is that we actually learned how to implement the interface that we needed to implement in order to create an ordered dynamic array of rectangle objects. The rest of this is pretty straightforward. So, we will add a few new rectangles. Let's add a three by four rectangle, a two by two rectangle, and a one by two rectangle. Finally, we'll print out the rectangles in the array to make sure that they ended up in the array in sorted order, the way we expected them to. So, I'll just Console Wrightline the rectangles ToString, and if I control F5 at this point, we see that we get the smallest area rectangle first, and then the middle area rectangle next, and then finally, the largest area rectangle. So, the ordered dynamic array worked properly, given the fact that we implemented the ComparedTo method in the Rectangle class the way we had to do based on the IComparable interface. To recap, in this lecture, we learned how to implement an interface. And we also learned that we need to read the interface documentation to make sure we implement that interface properly.