Beruflich Dokumente
Kultur Dokumente
1 Course Outline
Course Outline
Introduction
Page 1
Course Outline
Page 2
Course Outline
Page 3
Object Oriented Techniques
What Is a Class?
What Is an Object?
Comparing Classes to Structs
Abstraction
What Is a Class?
Page 4
Object Oriented Techniques
What Is an Object?
Page 5
Object Oriented Techniques
Abstraction
Using Encapsulation
Page 6
Object Oriented Techniques
Page 7
Object Oriented Techniques
Object Data
Page 8
Object Oriented Techniques
Page 9
Object Oriented Techniques
""
""
##
##
!!
"
$ "
""
## $$
$$
%% ##
%% &&&&&&"
&&&&&&"
Page 10
Object Oriented Techniques
'' ((
##
! ""
!!
""
%% ##
%%
&& ""
Page 11
Object Oriented Techniques
""
## (( ))
!!
""
(( ))
Inheritance
Class Hierarchies
Single and Multiple Inheritance
Polymorphism
Abstract Base Classes
Interfaces
Early and Late Binding
Page 12
Object Oriented Techniques
Inheritance
$ %
&" % !! # '
"" ( "
Class Hierarchies
" ))
))
** "
**
Page 13
Object Oriented Techniques
&& '' ))
&& ''
**
! ' # $
Polymorphism
&& '' ))
""
++ ,, -- ""
$$ **
++ ,, -- ++ ,, --
Page 14
Object Oriented Techniques
$$ **
00 11 00 11
Interfaces
)) 22 '' "" 33
00 11 ++
33
)) "" 33
&& '' )) ++
.. // 33
)) "" "" 33
** ++
00 11 33
Page 15
Object Oriented Techniques
))
00 11
++ ,, -- 4 '
**
00 11
++ ,, -- 5 '
Page 16
Object Oriented Techniques
Value types )
The variable
contains the
value directly #
"
Examples:
char, int
## ,-
,- ,,
## *+
*+ '' ((
%% &&
66
66 ,-,-
6 63(3( 7 736368 8
'' ** ++ && %% &&
6 63 3 9 93838
)
$$ ##
Page 17
Object Oriented Techniques
Invalid References
'' ++ ,, && ,,
'' ++ ,, && ,,
Page 18
Object Oriented Techniques
+ ''
&& -- .. **
& ''
$#
$#
++
$$ .. ## ++ // $$ %% ## 00 11
++ ## $$
22 33 $$ .. 44 ,, 55 ,, 44 $$ %%
22 33 ++ .. 44 ,, 55 ,, 44 ++ %%
.44
.44 %44
%44
.. ## ++ %% ## //
!! 22 %6
''
%6
22 33 .. 44 ,, 55 ,, 44 %%
Page 19
Object Oriented Techniques
Exception Class
String Class
Common String Methods, Operators, and Properties
String Comparisons
String Comparison Operators
Exception Class
Exception is a class
Exception objects are used to raise exceptions
Create an Exception object by using new
Throw the object by using throw
Exception types are subclasses of Exception
Page 20
Object Oriented Techniques
String Class
#
# ,-
,- ,,
789
789 #
#:::
: ;;
;; 22 <<
Brackets
Insert method
Length property
Copy method
Concat method
Trim method
ToUpper and ToLower methods
Page 21
Object Oriented Techniques
String Comparisons
Equals method
Value comparison
Compare method
More comparisons
Case-insensitive option
Dictionary ordering
Locale-specific compare options
## ,, ,,
## ,, ,,
== ##
## ;;
;; >>
Page 22
Object Oriented Techniques
11
## // 00
## //
Page 23
Object Oriented Techniques
Common Methods
Reflection
Page 24
Object Oriented Techniques
Data Conversions
Implicit conversions
Explicit conversions
Cast operator
Exceptions
System.Convert class
Handles the conversions internally
Page 25
Object Oriented Techniques
Parent/Child Conversions
The is Operator
==
## ;;
;; '' ==
22 33 ,(
,( ,,
Page 26
Object Oriented Techniques
The as Operator
## ;;
;; 22
== ##
##
22 33 ,(
,( ,,
?? ..
.. ## ## ..
.. ## ?? ## ..
.. ## ??
Page 27
Object Oriented Techniques
## $+/
$+/
?? .. ++ && -- ## ..
.. ##
'' ++ && --
Page 28
Object Oriented Techniques
2.3 Inheritance
Deriving Classes
«« 22
22 @@
00
«« 22
00
Page 29
Object Oriented Techniques
AA
BB
22 @@
((
22 @@
22 @@
Page 30
Object Oriented Techniques
Implementing Methods
33 ((
## "" ((
Page 31
Object Oriented Techniques
Overriding Methods
((
22 @@
## ((
Page 32
Object Oriented Techniques
33 ((
((
22 @@
## 33 ((
## ((
3
3 /
3
33 ((
22 @@
33 ((
Page 33
Object Oriented Techniques
33 ((
((
22 @@
33 ((
((
"" 22 ,, ,,
@@
"" 22 ,, ,,
2@
2@
"" 22 ,2,
,2,
@@ 22
"" 22 ,, ,,
""
## 22 ## ## ##
"" "" "" ""
Page 34
Object Oriented Techniques
""
BB %'
%' @@ ''
Using Interfaces
Declaring Interfaces
Implementing Multiple Interfaces
Implementing Interface Methods
Implementing Interface Methods Explicitly
Quiz: Spot the Bugs
Page 35
Object Oriented Techniques
Declaring Interfaces
'' CC
$$
44 22
33 ((
(( 55 66 77 88
66 77 88
66 66
Page 36
Object Oriented Techniques
@@ CC 55 C6
C6 ##
##
(( ##
##
C6
C6
(( %% ((
** C6
C6
Page 37
Object Oriented Techniques
;; <<
""
Page 38
Object Oriented Techniques
/ +
== CC
(( 44 22
@@ CC
CC ((
;; <<
22 @@ 00 ==
DD %% @@ 44 22 44 22
((
;; <<
22 @@ 55 CC 00 ==
DD %% @@ 55 CC 44 22 44 22
Page 39
Object Oriented Techniques
Similarities
Neither can be instantiated
Neither can be sealed
Differences
Interfaces cannot contain any implementation
Interfaces cannot declare non-public members
Interfaces cannot extend non-interfaces
## "" ((
33
22 @@
((
33
Page 40
Object Oriented Techniques
2.4 Operators
Using methods
Reduces clarity
Increases risk of errors, both syntactic and semantic
%C
%C 66 $$ ## CC %C
%C 66 +5
+5
CC CC %C
%C 66 /5
/5
%C
%C 66 ** 55 //
//
Using operators
Makes expressions clear
%C
%C 66 $$ ## %C
%C 66 ++ 44 %C
%C 66 // 44 %C
%C 66 ** 44 //
//
Page 41
Object Oriented Techniques
Predefined C# Operators
1 0
Arithmetic Member access
Operator Overloading
Page 42
Object Oriented Techniques
Operator overloading
Define your own operators only when appropriate
Operator syntax
Operatorop, where op is the operator being
overloaded
Example
"" (( $$ ++ (( $$ )) (( $$
,,
-- "" .. "" ++ "" //
00 "" .. $$ "" ++ $$ "" //
"" (( $$ -- "" )) 00 "" //
11
Page 43
Object Oriented Techniques
.. ==
.. == $$
$$
Page 44
Object Oriented Techniques
44 $5
$5
44 $5
$5 ==
<< $5
$5
<< $5
$5 ==
2.5 Attributes
Overview of Attributes
Introduction to Attributes
Applying Attributes
Common Predefined Attributes
Using the Conditional Attribute
Using the DllImport Attribute
Using the Transaction Attribute
Page 45
Object Oriented Techniques
Introduction to Attributes
Attributes Are:
Declarative tags that convey information to the
runtime
Stored with the metadata of the element
Applying Attributes
77 EE 99
Page 46
Object Oriented Techniques
72
72 ,, FF G))C(),
G))C(), 99
"%"
"%"
Restrictions on Methods
Must have return type of void
Must not be declared as override
Must not be from an inherited interface
Page 47
Object Oriented Techniques
"%2
"%2
## "%B
"%B ,-
,- GG 22 ,,
FF ''
77 AA >> II 99
"%
"% 22 @@ '' 22
Page 48
Object Oriented Techniques
77 GG "" 99
"%
"% @@ '%
'%
Page 49
Object Oriented Techniques
Page 50
Object Oriented Techniques
Page 51
Object Oriented Techniques
'%
'% >> == "" CC == %% CC ==
%% CC == ## %% == "%2
"%2
?? 77 99 ## %% CC == )) 22 ==
Page 52
Object Oriented Techniques
Overview
Delegates
Multicast Delegates
Events
When to Use Delegates, Events, and Interfaces
Delegates
Delegate Scenario
Declaring a Delegate
Instantiating a Delegate
Calling a Delegate
Page 53
Object Oriented Techniques
Delegate Scenario
+ >0
# 1
?
1 @
5 1
1 @
& >1 @
1 @ 0
- > ? % >1 @ 0
1 @ 0 ?
Declaring a Delegate
;;
;; ==
;;
;; == %% %%
"%
"%
Page 54
Object Oriented Techniques
Instantiating a Delegate
;;
;; --
;;
;; "%2
"%2
"%
"% ## "%
"% "%2
"%2 --
;;
;;
;;
;; "" ??
"%2
"%2 ## "%2
"%2
"%
"% ## "%
"% ""
Calling a Delegate
;;
;;
;;
;; 55 == "%2
"%2
;;
;; -- ,, ,,
,, ,,
Page 55
Object Oriented Techniques
Multicast Delegates
1 @ 1 @ 0
1 @ 0
& >1 @
- > +
+ +? 1 @ 0
. >1 @ 0
$
&?
++
Page 56
Object Oriented Techniques
;;
;; ==
## "%
"% 22 55
;;
;; == ==
## "%
"% >> 55
;;
;; CC ::
;;
;; ..
79
79 33 ## )) CC 33
== ## 88 KK 33 33 44
44
== 33 77 99 L# B
L# B $$
"%
"% 33 77 99
Page 57
Object Oriented Techniques
C# Language-Specific Syntax
Delegate Details
;;
;; "%
"% //
"%
"% // @@ '%
'% ""
"%
"% // ?? ?5
?5 == ==
@@ ?5
?5 == ;;
;;
CC ;;
;;
Page 58
Object Oriented Techniques
Events
Event Scenario
Declaring an Event
Connecting to an Event
Raising an Event
.NET Framework Guidelines
Declaring an Event
""
;;
;; "" 22
"" 22 FF --
"" 22
;;
;;
Page 59
Object Oriented Techniques
Connecting to an Event
;;
;;
;;
;; 22 "" 22
"" "" 22 4#
4#
"" 22 FF -- "" 22
;;
;; 22 "" 22
"" "" 22 <#
<#
"" 22 FF -- "" 22
Raising an Event
== "" 2
2 L#
L#
"
" 2
2
Page 60
Object Oriented Techniques
'' BB FF --
?? 55 '' BB FF
'' BB FF -- '' BB
Page 61