Академический Документы
Профессиональный Документы
Культура Документы
C# 30
C# 3.0
Chapter 12 Interfaces
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 2
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 3
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 4
C# 30
Interfaces Introduction
Yo
You want
ant this ffunctionality
nctionalit to serve
ser e as a
contract:
A type that claims to adhere to the contract must actually
p
all the methods defined in the contract
implement
As a client that uses such a class, you want to be ensured
that all methods are implemented according to their
definition in the contract
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 5
C# 30
Interfaces 6
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 7
C# 30
Interfaces 8
C# 30
Interfaces Vs.
Vs Abstract Class
Looking at an interface definition
Isnt it actually a class?
To be more specific, an abstract base class that contains
only pure virtual methods?
An interface
f
is only a behavior definition
f
A set of related abilities, just signatures, with no
iimplementation
l
t ti
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 9
C# 30
Interfaces Vs.
Vs Base Class
A base class will
ill be defined either as
as:
A regular class : providing common functionality,
or as an interface: defining behavior only
In addition:
A derived class extends a base class
It extends and specializes its base structure and functionality
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 10
C# 30
Interface Declaration
In C#,
C# an interface is not a class with
ith
p
characteristics
special
It is a first-class built-in concept
having rules and conventions for definition and
implementation
A
An interface
i t f
iis d
defined
fi d using
i th
the kkeyword
d
interface
By convention, its name should be preceded with a
capital I
I followed by the interface name with the first
letter capitalized
IEmbeddable,IEnumerable
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 11
C# 30
Interface Declaration
An interface can contain methods
methods,
properties, indexers and events, none of
which
hi h are iimplemented
l
t d iin th
the iinterface
t f
itself
An interface is a reference type that is never directly
p
created and has no actual representation
Interface method declarations should not be prefixed
with an access modifier
They are all, by default, public
Interface methods are all,, byy default,, pure
p
virtual
methods
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 12
C# 30
Interface Declaration
An interface can contain also properties
properties,
indexers and events
The following is an enhanced version of the
IEmbeddable interface
interfaceIEmbeddable
{
voidLoad();
voidPlay();
y();
stringTitle{get;}
tCli k dE
t Clicked;
Cli k d
eventClickedEvent
stringthis[int index]{get;set;}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 13
C# 30
Interface Implementation
A class that is defined to implement an
interface must define all members
declared in the interface:
Interface method signatures cannot be modified
Interface methods must be defined as public
Interface methods can be defined to be virtual
Interfaces 14
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 15
C# 30
An abstract
A
b t t base
b
class
l
Shape,
h
d fi i shapes
defining
h
common
fields position and methods Draw(),Resize() and
Move()
()
Three derived classes : Circle, Rectangle and
Triangle, each overrides required methods as well as
adding its specific functionality
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 16
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 17
C# 30
publicclassRectangle:
Shape IDraw3D
Shape,IDraw3D
{
publicvoidDraw3D()
publicvoidDraw3D()...
}
Interfaces 18
C# 30
Lets
Let s start with a simple attempt:
Interfaces 19
C# 30
Interfaces 20
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 21
C# 30
C# 30
Interfaces 22
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 23
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 24
C# 30
Interfaces 25
C# 30
Interfaces 26
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 27
C# 30
Interfaces 28
C# 30
Interfaces 29
C# 30
AtleastoneobjectmustimplementIComparable.
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 30
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 31
C# 30
Interfaces 32
C# 30
Extension Methods
Extending an Interface
//InSystem.dll
//I
S t
dll
publicinterfaceIComparable {
int CompareTo(objectobj);
p
( j
j);
}
//InMyExtensions.dll
namespaceMyExtensions
M E t
i
{
staticclassComparableExtensions {
publicstaticbool IsGreater(this
p
(
IComparable
p
obj,
j,
objectother){
returnobj.CompareTo(other)>0;
}
publicstaticbool IsEqual(this IComparable obj,
objectother){
j
) {
returnobj.CompareTo(other)==0;
}
}
}//endnamespace
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 33
C# 30
Extending
Extending an Interface
//InBigInt.dll
classBigInteger :IComparable {
//Implementation
}
//Inmycode:
usingMyExtensions;//Importextensions
BigInt b1=newBigInt(10000000);
BigInt b2=newBigInt(10000001);
b1.IsEqual(b2);//CompareExtensions.IsEqual
b1.IsGreater(b2);//CompareExtensions.IsGreater
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 34
C# 30
Interfaces 35
C# 30
Interfaces 36
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 37
C# 30
Interfaces 38
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 39
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 40
C# 30
Interfaces 41
C# 30
Enumerator Example
classEmployeeManager :IEnumerable
{
//
//IEnumerable requiresthisfromanenumerableclass:
//ItreturnsanobjectofIEnumerable type,
//thatprovidestheactualenumerationservices.
publicIEnumerator GetEnumerator()
{
returnnewEnumerator(employees);
}
//
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 42
C# 30
Enumerator Example
//Nestedclass
publicclassEnumerator:IEnumerator {
publicEnumerator(IList employees){
_employees=employees;
ResetCurrent();
}
publicobjectCurrent
{
get{
if(
(_cu
current==1||
e t
|| _cu
current>
e t > _e
employees.Count1)
p oyees.Cou t )
thrownewInvalidOperationException();
return_employees[current];
}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 43
C# 30
Enumerator Example
publicbool MoveNext(){
if(_current<_employees.Count 1)
{
_current++;
returntrue;
}
returnfalse;
}
publicvoidReset(){ResetCurrent();}
privatevoidResetCurrent(){
_cu
current=1;
e t
;
}
privateint _current;
privateIList _employees;
}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 44
C# 30
IEnumerable<T> IEnumerator<T>
IEnumerable<T>,IEnumerator<T>
id ttype safety
f t and
d performance:
f
T
To provide
publicinterfaceIEnumerable<T>:IEnumerable {
IEnumerator<T>GetEnumerator();
}
publicinterfaceIEnumerator<T>:
p
IDisposable,IEnumerator {
TCurrent{get;}
}
Interfaces 45
C# 30
All .NET
NET collections implement several interfaces that
represent basic element management, such as the
ICollection &ICollection<T>,IEnumerable
,
&
IEnumerable<T>,ICloneable interfaces
Interfaces 46
C# 30
Interfaces 47
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 48
C# 30
IFormattable Example
classEmployee:Iformattable {
privateint _id;
privatestring name;
privatestring_name;
privateint _salary;
privateint
p
ate
t _se
seniority;
o ty;
privatechar_gender;
//
publicoverridestringToString()
{
returnString.Format("Id:{0},Name:{1},Salary:{2}",
_id,_name,_salary);
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 49
C# 30
IFormattable Example
publicstringToString(stringfmt,IFormatProvider
publicstringToString(stringfmt
IFormatProvider fp)
{
switch(fmt)
{
case ( u ):
case(null):
returnToString();
case"F":
returnString.Format(...);
default:
returnToString();
}
}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 50
C# 30
IFormattable Example
usingSystem;
classEmployeesApp {
staticvoidMain(){
Employeee1=newEmployee("Jack",10000,'M');
Employeee2=newEmployee("Jane",12000,'F');
Console.WriteLine("Employeeno.1{0}",e1);
Console.WriteLine("Employeeno.3{0:F}",e2);
}
}
Employeeno.1:Id:0,Name:Jack,Salary:M
p y
,
,
y
Employeeno.2:
Id:1
Name:Jane
Gender:F
Seniority:0
Salary:12000
Copyright
SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Interfaces 51
C# 30
Chapter 12 Exercise 1
Interfaces 52
C# 30
Chapter Summary
Interfaces define contracts
Classes define behavior
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel