C# Interfaces and RTTI CNS 3260 C#.NET Software Development
Interface “A surface forming a common boundary between adjacent regions, bodies, substances, or phases” – Dictionary.com
C# Interfaces Defines object behavior Methods Properties Indexers Events Not Variables classes and structs may implement interfaces Syntax is like inheritance Multiple interfaces may be implemented Each Interface member must be implemented if the class implements the interface
Interface Syntax public interface IPlayer { void Play(); } Notice: interface keyword No access specifier all members of an interface are inherently public No function body allowed No implementations allowed Practice is to start the interface name with an “I”
Interface Inheritance Interfaces may inherit from other interfaces Any class implementing the interface must implement all the methods of all the interfaces in the inheritance chain public interface IRadioPlayer : IPlayer { float Volume { get; set; } }
Implementing Interfaces Interface members must be implemented publicly Implementing an interface adds the interface type to the implementing class MyRadio is-a IRadioPlayer public class MyRadio : IRadioPlayer { private float volume = 50f; public float Volume { get { return volume; } set { volume=value; } } public void Play(int index) { // TODO: Implement Play }
With class Inheritance... Classes may inherit from a class and still implement interfaces Class inheritance comes first syntactically public class MyEntertainmentCenter : MyRadio, IComparable, IServiceProvider { IComparable Members IServiceProvider Members }
Name Conflicts What if two Interfaces use the same member name??? public interface ICar { void Go(); } public interface IBoat { void Go(); }
The Infamous Car-Boat public class TheInfamousCarBoat : ICar, IBoat { public void Go() { Console.WriteLine("Going... (Wet or dry? I don't know.)"); } The Implementation of Go() satisfies both interfaces Reference of either ICar or IBoat will call Go()
The Amphibious Vehicle Implements each explicitly private to the class public to the appropriate Interface An interface reference will call the appropriate method public class AmphibiousVehicle : ICar, IBoat { void IBoat.Go() { Console.WriteLine("IBoat.Go()... Floating"); } void ICar.Go() { Console.WriteLine("ICar.Go()... Driving"); } (See Interface Demo)
Virtual and override Interface functions are not virtual It wouldn’t make any sense You can however make them virtual in the implementing class An override function may satisfy an interface A public function in a base class before the interface will satisfy the interface in a derived class (See Interface Demo) Normal overriding/hiding rules apply to the derived class
The Proper use of Interfaces Allow you to separate object definition from implementation Once an interface is set, changing it can be a big deal Especially if there is code programmed to the interface already Interface IBetween Programmer A creates Module A implements IBetween Programmer B creates Module B uses an IBetween
Interfaces in the Libraries IComparable Allows to items to be compared (and thus, sorted) IComparer Create a custom compare object to override IComparable for standard objects ICloneable Allows objects to create a deep copy of themselves IDisposable The class implements a Dispose method ISerializable Defines how an object may write itself out to disk (serialize itself) IEnumerable Indicates that the class can be traversed like an array (using foreach) And a whole bunch more