Sponsored Links

Inheritance and Interfaces

C# is an object oriented programming language meaning it works by declaring and passing around objects with a defined parent child heirarchy of inheritance. C# implements single inheritence, which means a class can only have one parent class. A child class inherits all of its parent's properties and methods (but it can overwrite them with different declarations). If you want to declare that one class inherits from another then simply type a colon followed by the parent class name after the child class name definition (e.g. public class MyChildClass : MyParentClass { } ).

The fact that classes can only inherit from one parent class might cause issues if you wanted a bit more flexibility in what you could pass into or return from a function. That's where interfaces come in - they let me define a set of functions and parameters that a class will implement so that you can use them when processing a class that implements the interface. The clasic example for understanding this is to use animals; if you had a class "Mammal" and two more classes "Cat" and "Dog" then Cat and Dog could both inherit from Mammal. If you had an interface ObeyVoiceCommands then the Dog could inherit from Mammal and implement ObeyVoiceCommands. Now a function could return an object implementing ObeyVoiceCommands and call on any of those commands for the appropriate response.

Interfaces are declared in the same way as classes (public interface IObeyVoiceCommands{ ... }) except that they should only have abstract method declarations (that is, method declarations with no implementation). You also declare that a class implements an interface in the same way as declaring a class inherits from another one - use the colon followed by the interface name after the class name declaration. If you need to implement multiple interfaces or inherit from one and implement from others then just comma seperate the parent class / interface names (e.g. public class MyChildClass : MyParentClass, IObeyInteraceA, IObeyInterfaceB {...} ).

Personally, I think of inheritance modelling an "Is a... " relationship and interfaces modelling a "Can do..." relationship.

Inheritance and Interfaces in the .NET Framework

Inheritance is used in far too many places in the .NET framework to list - just look at the exception heirarchies as an example of that. There are a couple of important interfaces that you should be aware of:

  • IEnumerable: Every time you use the "foreach" loop, you're calling on the methods of IEnumerable. Arrays implement it, List<T> implements it and it's the return type of a LINQ query. Anything that implements IEnumerable can be looped through using for each.
  • IDisposable: Pretty easy to understand - if it implements IDisposable then you can call Dispose on it.
  • IComparable: A type that implements this can be given an order in relation to others of that type. This interface needs to have been implemented if you want to call Sort on an array.
  • ICloneable: The type can be cloned
  • IEquatable: The type can be checked for equality (the == operator can be applied)

Partial Classes

Partial classes are pretty much what they sound like: they define the class in pieces. If you look at an aspx.cs class in ASP.NET you'll notice that the class definition for the page handler is defined with "public partial class MyPage..." and it just contains the methods for handling the Page load / postback. The rest of the class is defined in the .aspx.designer.cs which contains the more standard stuff that would clutter your code, like control declarations. It's easy to understand what's going on (the classes are merged before compilation) but personally I wouldn't use this myself as I think it would only serve to obfuscate code down the line.