Microsoft Exam 70–483: Programming in C# — Objective 2.4: Create and implement a class hierarchy
Hello, folks!
In this story, the Objective 2.4: Create and implement a class hierarchy will be discussed! Hope you will enjoy it.
In the last story, the Objective 2.3: Enforcing encapsulation was presented.
All the code for the certification series can be found here.
Design and implement an interface
Quoting the Microsoft Programming Guide for C#, “ An interface contains definitions for a group of related functionalities that a class or a struct can implement.”. That is, interfaces can be used to define and enforce a contract. Any class that implement an interface (the same way inheritance is defined) must express its method signatures and property definitions.
Interfaces were discussed in the last story, when explicit interface implementation was presented. Classes can implement one or more interfaces.
Code example:
In the above code, it is defined a IPerson interface and its definitions. As it was said before, both classes that “inherits” from it must implement its definitions.
Inherit from a base class
Even though the interface enforces a contract, as it was shown before, it does not provide any implementations. The derived type is the one that is responsible for implementing it in the way it want (of course, following the definitions signature).
In cases when an inherited implementation is desirable, you use… yes, you guessed it right: inheritance.
The syntax for inheriting is the same of expressing the implementation of an interface. You can use the inherited implementation code by using the keyword base and calling the desired method from it. This is more interesting when overriding methods.
Code example:
In the above code, both the Student and Employee class inherit from the Person class. The method SayHi is overrided by the derived classes, changing its behavior when called from instances of these classes. In order to override a class implementation, add virtual to the property definition, as it was used in the code.
In case it is not desirable to allow a given class to be derived, the sealed keyword can be used when defining the class.
Also, if it is not desirable to allow the base class to be instantiated, the abstract keyword can be used.
Code example:
Implementing standard .NET Framework interfaces
There are some standard interfaces which the .NET Framework allows you to use when implementing your own classes.
Each of them are useful in different scenarios, and it is important to learn about them since you most likely are going to use most of them during your developer career.
IComparable
By implementing that interfaces it is allowed to implement custom ordering. The method CompareTo must be implemented, and it receives an object type as parameter, returning an int value.
If the returned value is less than zero, it means that the calling instance precedes the object passed as parameter in the sorted order. If it is greater than zero, in order hand, specifies that the calling instance follows (or is ahead) the object passed as parameter in the sorted order. If it is zero, it is in the same position.
This implementation is used when calling the Sort method of collection types such as the List and Array classes.
Code example:
In the above code, the Student class implements the IComparable interface. The CompareTo method uses the Score property to sort a collection of that type.
IEnumerable
By implementing the IEnumerable interface, the .NET framework allows a class to implement the iterator pattern, enabling it to be iterated as a collection. The logic regarding the iteration will be defined by the GetEnumerator method.
Code example:
In the above code, the Race class implements the IEnumerable interface, and it allows its internal RaceCar List to be used when the Race class is iterated through by using the foreach statement.
IDisposable
Implementing the IDisposable allows the developer to free unmanaged resources, such as database connections.
When you use the using statement in C#, your class needs to implement the IDisposable interface. At the moment it exits, the Dispose method is called.
More on IDisposable will be discussed on Objective 2.6: Manage the object life cycle.
IUnknown
The IUnknown is a COM interface. You can use it if you want to create your own wrapper class for the COM Interop. More on Using and Implementing Unknown, access here.
That’s it for now! I hope that you learned at least something useful whether you are studying focused on clearing the certification exam or just looking for learning more about C#.
See you in the next Story, where I will discuss the Objective 2.5: Find, execute, and create types at runtime by using reflection.
PS: If you find this Story useful, I invite you to hit the Clap button. Also, I would be happy to see you as my new follower!