Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Interfaces and RTTI CNS 3260 C#.NET Software Development.

Similar presentations


Presentation on theme: "C# Interfaces and RTTI CNS 3260 C#.NET Software Development."— Presentation transcript:

1 C# Interfaces and RTTI CNS 3260 C#.NET Software Development

2 Interface “A surface forming a common boundary between adjacent regions, bodies, substances, or phases” – Dictionary.com

3 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

4 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”

5 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; } }

6 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 }

7 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 }

8 Name Conflicts What if two Interfaces use the same member name??? public interface ICar { void Go(); } public interface IBoat { void Go(); }

9 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()

10 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)

11 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

12 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

13 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


Download ppt "C# Interfaces and RTTI CNS 3260 C#.NET Software Development."

Similar presentations


Ads by Google