c# interfaces c# class version 1.0. copyright © 2012 by dennis a. fairclough all rights reserved. 2...
TRANSCRIPT
C# Interfaces
C# Class
Version 1.0
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2
Interface
“A surface forming a common boundary between adjacent regions, bodies,…” – Dictionary.com
C# Standard
An interface type defines a contract as a named set of public method prototypes. A class or struct that implements an interface must provide implementations of the interface’s method prototypes. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 3
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 4
Interfaces in Programming The member(s) that a class or an object expose(s) is
its interface Classes can implicitly or explicitly share an interface
Classes which share common member(s) but are not related implicitly share an interface STL containers
Classes with a common type explicitly share an interface An explicit interface defines a contract
C# has such an interface construct!
Interface (Contract) Like a class except for:
Provides only a specification (prototype) for one or more member methods, properties, indexers, events. implicity abstract and public, implied &
required, i.e. virtual and non-static. Classes and struct’s can implement
(inherit) one or more interfaces “…we defined polymorphism as the ability to perform the same
operations on many types, as long as each type shares a common subset of characteristics. The purpose of an interface is to precisely define such a set of characteristics.”
(C# Essentials 2nd Edition)
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 5
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 6
C# Interfaces Define object signatures, names and return data types for:
Methods Properties Indexers Events Not Variables!
classes and structs may implement zero or more interfaces Accomplished through no inheritance, single, multiple
inheritance A class or struct may implement zero or more interfaces Each Interface member must be implemented in the class
that implements that interface
C# Standard Interfaces Overview
An interface has the following characteristics: Similar to an abstract base class: any non-abstract type inheriting
the interface must implement all its members. Adapters are used to modify this constraint. Cannot be instantiated directly. Can contain events, indexers, methods and properties. Contain no implementation of methods. Classes and structs can inherit from zero or more interfaces. An interface can itself inherit from zero or more interfaces
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 7
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 8
Defining an Interfacepublic interface IPlayer{ void Play(); //method prototype}
Notice: interface keyword Only method prototypes are allowed No access specifier allowed on members
all members of an interface are inherently public No function bodies allowed
An interface has no method implementations Common practice is to start the interface name
with an “I” i.e. IPlayer
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 9
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
Multiple interface inheritance and hierarchies are allowed
public interface IRadioPlayer : IPlayer{
double Volume //property prototype{
get;set;
}}
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 10
Implementing Interfaces Interface members always
implemented publicly Implementing an interface
adds the interface type to the implementing class MyRadio is-a
IRadioPlayer MyRadio is-a IPlayer
public class MyRadio : IRadioPlayer{ private double volume = 50f;
public double Volume { get { return volume; } set { volume=value; } }
public void Play() { // TODO: Implement Play }}
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 11
With class Inheritance...
Classes may inherit from a class and still implement interfaces Class inheritance MUST come first
followed by the interface(s)
public class MyEntertainmentCenter : MyRadio, IComparable, IServiceProvider{
IComparable Members
IServiceProvider Members}
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 12
Name Conflicts
What if two Interfaces use the same member name? Not good practice!
public interface ICar{ void Go();}
public interface IBoat{ void Go();}
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 13
The Infamous Car-Boatpublic 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 for either ICar or IBoat will call Go()
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 14
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 Interfaces Demo)
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 15
virtual and override Interface methods are not virtual
It wouldn’t make any sense You can however make them virtual in the implementing class
A override or new modified method may satisfy the interface
A public function in a base class before the interface will satisfy the interface in a derived class
Normal overriding/new hiding rules apply to the derived class
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 16
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
Adapters can be used to solve this problem
InterfaceIBetween
Programmer Acreates
Module AimplementsIBetween
Programmer Bcreates
Module Buses an
IBetween
Interface ValueType or Reference Type Variables
ValueType or Reference Type Variables my be cast to an interface type.
IEnumerable inumb = new MyObject(); What is the implication?
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 17
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 18
Interfaces in the C# Libraries
IComparable Provides for items to be compared (and thus, sorted)
IComparer Provides for objects to be compared and to override IComparable for
standard objects ICloneable
Provides for objects to create a copy of themselves IDisposable
Forces the class to implement the Dispose method ISerializable
Provides for an object to write itself out to disk (serialize itself) IEnumerable
Provides for a class to be traversed like an array (using foreach) And a whole bunch more!
IEnumerable & IEnumerator
See Example Code & Lab
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 19
Copyright © 2012 by Dennis A. Fairclough all rights reserved. 20
What did you learn?
??