WHAT'S NEW?
Loading...

Day 7 Programming in C# 70-483

Index


  • Introduction
  • Inheritance with Interfaces
  • Inheritance with base classes
  • Standard .Net interface implementations
  • References

Introduction

This post is part of a series of post to help you prepare the MCSD certification, particularly the certification exam 70-483, based on the book:


You will find all the code available in the following GitHub repository. Lets talk a little bit about threads and how they work using a .Net development environment.

Inheritance with Interfaces

Inheritance is the process of derive one class form another. For example, you can have the class Animal (base class) and another class called Dog (derived class) and this relation allow the derived class to access to the base class methods and properties. Multiple class inheritance is not supported.

Interfaces are really important in C# because they are used as a contract the users can not break. Interfaces cannot be instantiated. This contract contains the signatures of methods, properties, indexers and events which are implemented on concrete classes. Interfaces have to start with capital "I" and followed by a descriptive name like "IOrdersManagement". There are no access modifiers and all elements are public by default. You can't change this in the interface implementation either. Multiple interface inheritance is supported.

In the following example we have an interface containing a property with just has a get accessor. The concrete class which implements the interface defines both get and set accessor. This ends up with users of the interface just accessing the get and users of the concrete accessing the get and set.


Interfaces can also inherit from other interfaces, then the derived class needs to implement all the derived elements. You can also use generics which is the typical approach when you want to implement the Repository pattern (used to access a data storage system and perform operations with the information).  See here a small example where we have an interface with a generic type defined by "T" which basically helps us to pass any entity (Order, Invoice, Quote...) we want and performs a mapping with a data storage.


Bare in mind that you should program against interfaces not concrete classes. You don't need to care about how something works, just to know that it works.

Inheritance with base classes

We've seen how can we use generics to create a class which performs lookups against a data storage following the Repository pattern. In the following example you can see how to implement that interface:


What if we want to do something explicit for a particular entity, for example, if we want to add a method to search between orders by date and amount. Then, you can use inheritance to reuse your code while adding some extra behaviour. Here we defined an order entity which is used in our own OrderRepository class which is a more customized version of our old Repository class.


You can also replace/extend the behaviour of your base classes by using virtual (base class) and override (derived class) keywords. There another decorator called new which hides the virtual method in the base class but this one is not recommended to use. 

Abstract classes are something in between of Interfaces and regular classes. They can have implemented methods (not required) and also abstract elements that need to be implemented in the derived class by using the override decorator. They can't be instantiated.

On the other hand are the sealed classes. They prevent other class to inherit from them. Structs are implicitly sealed in C#. 

When talking about inheritance in a OOP language you better bear in mind the Liskov substitution principle (remember the SOLID principles) which says that every derived class could be used in place of the base class without changing the behaviour in any of the cases.

Standard .Net interface implementations

There are some interfaces provided by .Net which can help you to define some typical behaviours required in OOP languages. See the following examples:
  • IComparable: it contains just a method which helps you to stablish if two objects are the same or not. It returns an integer type and if its value is zero means both objects are the same. In case of returns less than zero means that the current precedes the object specified. Greater than zero means the current object follows the object specified. See the following implementation:
  • IEnumerable: it helps you to implement the iterator pattern in an array, list or your own collection. The interface exposes a GetEnumerator method that returns an enumerator, this can be used to move to the next element by calling MoveNext().
  • IDisposable: this is a very useful interface used to release resources like files or database connections, which are known as unmanaged resources. The garbage collector can handle these kind only by telling him how to do that implementing this interface. It contains only a method called Dispose(). 
  • IUnknown: not very common nowadays but it turns handy when you need to make use of a particular COM reference but the COM interop classes fails the build by the compiler.

0 comments:

Post a Comment