Vous êtes sur la page 1sur 8

C# 30

Operator Overloading 1

C# 3.0
Chapter 13 Operator Overloading

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 2

Operator Overloading
O erloading
Operator overloading is similar to C++:
Use intuitive operator syntax on user-defined types
Methods are defined as operators using the
operatorkeyword
Operators
O
t
are overloaded
l d d only
l when
h it makes
k
immediate sense

C# operator overloading is simpler (and


more limited) than in C++

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 3

Operator Overloading Example


classDate{
{
...
publicstaticbool operator==(Dated1,Dated2){
returnd1._day==d2._day&&...;
t
d1 d d2 d &&
}
p
p
(
,
) {
publicstaticbool operator>(Dated1,Dated2){
...
}
publicstaticbool
bli t ti b l operator<(Dated1,Dated2){
t <(D t d1 D t d2){
return(d2>d1);
}
publicstaticDateoperator+(Dated1,int numDays){
DateretVal =d1;
tV l I
tD
(
D
)
retVal.IncrementDays(numDays);
returnretVal;
}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 4

Operator Usage
classDateApp
pp {
staticvoidMain(){
Dated1=newDate(1,1,2000);
D t d2 d1
Dated2=d1;
if(d2== d1)
( q
);
Console.WriteLine ("Equal");
Dated3=newDate(1,1,2000);
if(d3!= d1)
C
Console.WriteLine
l W it Li
("N tE
("NotEqual");
l")
d1++;
++d1;
;
Dated4=d1+ 5;
d4+= 6;
}
}

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 5

Operator Overloading
Operators should
sho ld be defined as static
class methods
At least one of the parameters must be the type
containing the operator
Cannot modify the syntax, precedence, or
associativity of an operator

A
Assignment
i
and
d equality
li are automatically
i ll
implemented for all types
Value types and reference types are different, of
course
course
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 6

Which Operators Are Available


The follo
following
ing unary
nar operators can be
overloaded:
+, -, !, ~, ++, --, true, false

The following binary operators can be


overloaded:
+, -, *, /, %, &, |, ^, <<, >>

The comparison operators can be


)
overloaded ((in pairs):
==, !=, <, >, <=, >=

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 7

What Operators Are Available


The array indexing operator [] cannot be
overloaded, but you can define indexers
Compound assignment operators cannot
be overloaded (automatic):
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

The following operators cannot be


overloaded:
l d d
=, ., ?:, ->, new, is, sizeof, typeof,
&&, ||
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 8

Overriding Equality
When o
overloading
erloading eq
equality,
alit o
override
erride
q
and GetHashCode appropriately
pp p
y
Equals
Ensure that if a.Equals(b) then a.GetHashCode()
==b.GetHashCode()
()
Ensure that theres a good distribution of hash values
Ensure that operator== and Equalsare in sync

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 9

Operator Names in the CLS


Operator Sign
+ (binary)

Operator Name
op_Addition

- (binary)
(bi
)

op_Subtract
S bt t

==

op_Equality

!=

op_Inequality

>

op GreaterThan
op_GreaterThan

+=

op_AdditionAssignment

++

op_Increment
I
t

Conversions
+ (unary)

op_Implicit
op_Explicit
op
UnaryPlus
op_UnaryPlus

- (unary)

op_UnaryNegation

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 10

Operator Notes
Compound arithmetic (e
(e.g.
g +=) operators
are automatically generated
Theres
There s just one increment operator and
one decrement operator
Postfix and prefix semantics are automatically
implemented

The function call operator


p
is not available

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 11

User-Defined Conversions
Users can define conversions between
user-defined types and other types
But not two system types

Conversions are defined as static


f
functions
ti
One of the parameters must be of the enclosing
types type
Declared either explicit or implicit

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 12

Conversions Example
struct Celsius{
{
...
publicstaticimplicitoperatorFahrenheit(Celsiusc){
return(((c._temp
t
(((
t
*9)/5) 32)
*9)/5)+32);
}
p
p
p
(
p) {
publicstaticimplicitoperatorCelsius(floattemp){
Celsiusc;
c=newCelsius(temp);
returnc;
t

}
p
publicstaticimplicitoperatorfloat(Celsiusc){
p
p
(
) {
returnc._temp;
}
}
//Fahrenheitstruct definitionisverysimilar

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 13

Explicit and Implicit Conversions


Declare a conversion explicit if it can
cause exceptions or lose information
(
(precision)
i i )
An explicitconversion must be invoked via an
explicit cast operator

Implicit conversions should not cause any


unexpected
t d results
lt
No exceptions, no precision loss

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 14

Sequence of Conversions
The compiler will perform a sequence of at
most two conversions of the form:
Basic conversion followed by user-defined
conversion, -or User-defined conversion followed by basic conversion

For a Celsius object that has a conversion


from float
float, this is a valid sequence:
Celsiusc=...;
c=2;
//Implicitint tofloat,implicitfloattoCelsius

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

C# 30

Operator Overloading 15

Chapter Summary
C# operators are
Static methods
Must have at least one argument of its enclosing
types
yp
type
yp
Can be explicit or implicit

Some operators are automatic


Beware with redefining equality and
defining new conversions

Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel

Vous aimerez peut-être aussi