Documente Academic
Documente Profesional
Documente Cultură
C# 30
C# 3.0
Chapter 4 The C# Classes
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 2
C# 30
A C# Class Example
In the next
ne t slides,
slides youll
o ll find an e
example
ample of
Employee.
p y
Read it!
a C# class: Employee
What seems to be similar to a C++ class definition?
What seems to be different?
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 3
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 4
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 5
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 6
C# 30
The C# Classes 7
C# 30
Access Modifiers
C# access modifiers
Accessibility
Meaning
public
protected
internal
private
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 8
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 9
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 10
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 11
C# 30
Fields
Constants
Methods
Constructors
Finalizer
Properties
Indexers
Operators
Events
E
t
The C# Classes 12
C# 30
Fields Initialization
A field is what
hat we
e kno
know from C++ as a
data member
By default, class fields are initialized with a default
value according to their type category
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 13
C# 30
Constructors
Like in C++:
The constructor is a special method bearing the
class name and must not return any value
A class constructor is activated before an instance of
the class is created
It should be used to provide the instance fields with
appropriate initial values
Th
The constructor
constructor
t
t s initialization
i iti li ti list
li t in
i C#
is used differently:
y
It cannot be used for fields initialization
It can be used only for other constructors
constructors activation:
The class base constructor
Another constructor in the class
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 14
C# 30
Constructors Example
classEmployee
{
publicEmployee(stringname,int salary)
{
_name=name;
a e
a e;
_salary=salary;
}
publicEmployee(stringname)
:this(name,_minimumSalary)
{
}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 15
C# 30
Instance vs
vs. Static Fields
B
By default,
defa lt a field is defined to be an
instance field:
A field that is contained in every instance of the class,
holding an instance
instancess specific value
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 16
C# 30
Static Constructors
Static constructor is automatically created
to contain a simple static field initialization
The class author can explicitly define such a
constructor
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 17
C# 30
Constants
A constant is a field that remains constant
throughout the applications lifetime
A constant is defined using the keyword: const
A constant field is always static
The C# Classes 18
C# 30
Read-Only
Read
Only Fields
The constant field features might sometimes be
insufficient
We might want to define an unchangeable field that its value
cannot be computed at compile-time
We may also require this non-changeable field to be an instance
fi ld and
field
d nott a static
t ti fi
field
ld
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 19
C# 30
Properties
Properties can be considered as smart
fields
They enable defining special get and set methods,
termed getter and setter
These methods can be activated with the simple field
access syntax
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 20
C# 30
Properties Example
classDate{
publicDate(int
publicDate(
int day,int
day,int month,int
month,int year)...
publicint
public
int Day
{
get
get{return
{ etu
_day;
day;}
day;}
set{
(IsDayOK
y
(
))
if
if(
(value))
_day=value;
_day
=value;
}
}
}
//Usage:
Dated=newDate(...);
Console.WriteLine(
Console.WriteLine
(d.Day);
d.Day);
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 21
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 22
C# 30
Property Accessibility
By default,
default the accessibility of the set and
the get accessors is provided by the
property definition
An accessors accessibility level can be explicitly
changed to a more restrictive one
publicFieldType PropertyName
{
get
{
returnfield;
}
protectedset
{
field=value;
}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 23
C# 30
Automatic Properties
Trivial
automatically
Tri ial properties can be a
tomaticall
field
backed byy a compiler-generated
p
g
publicclassEmployee{
publicstringName{get;set;}
publicint
bli i
Id{get;set;}
d {
}
}
Accessibility
A
ibilit can be
b specified
ifi d as well:
ll
publicclassEmployee{
...
publicfloatCommission{get;internal set;}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 24
C# 30
Methods
Class member ffunctions
nctions are termed
termed:
methods
In C#, all methods are defined inside the class
Class methods can access other class members
publicvoidraiseSalary(int percentage)
{
salary+=salary*percentage/100;
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 25
C# 30
Static Functions
Functions
F nctions that manipulate
manip late static fields onl
only
Static methods can be activated through class name
only
Theyy have no access to anyy non-static
classEmployee{
publicstaticvoidraiseMinimumSalary(int percentage)
{
minimumSalary +=minimumSalary *percentage/100;
}
privatestaticint minimumSalary =6000;
}
staticvoidMain(){
Employee.raiseMinimumSalary(5);
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 26
C# 30
Static Class
A class can be declared as static b
by using
sing
y
in the class definition
the static keyword
It is not possible to create instances of a static class
The C# Classes 27
C# 30
Static Class
A static
t ti class
l
cant
t have
h
non-static
t ti
methods
There are several situations where
static classes are useful:
Wh
When group off methods
th d provide
id global
l b l functionality
f
ti
lit
A singleton pattern can be implemented by using a
static class
We need a type that represents a bunch of P/Invoke
methods
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 28
C# 30
The C# Classes 29
C# 30
Extension Methods
Challenges:
Challenges
How do you add functionality to a sealed class?
How do you add functionality to an interface without
changing
g g it or its implementors?
p
The C# Classes 30
C# 30
Whats
What s The Difference?
Discoverability
Disco erabilit
Intellisense support
We can write:
int i =s.ToInt
=s.ToInt();
();
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 31
C# 30
Limitations:
Extension methods can access public members only
Extension methods can live in static classes only
To use an extension method, its namespace must be
imported (usingMyExtensions;)
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
C# 30
The C# Classes 32
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 33
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 34
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 35
C# 30
Object Initializers
Settable properties often replace
g
to object
j
constructors with regard
initialization
iti li
t
Obj
Objectt iinitializer
syntax:
//Insteadof:
Personp=newPerson();
p.Id =2
=2;
p.Name =JohnDoe;
JohnDoe ;
Personp=newPerson{
Id=2
Id=
2,
Name=JohnDoe
};
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 36
C# 30
Object Initializers
Its possible to in
invoke
oke constr
constructors
ctors with
ith
parameters:
p
Personp=newPerson(
Personp=newPerson(2) {
Name=JohnDoe
};
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 37
C# 30
Chapter 4 Exercise 1
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 38
C# 30
Partial Types
A partial ttype
pe can be split o
over
er ttwo
o or
more source files
There are several situations where
splitting
litti a ttype iis desirable:
d i bl
When working
g with wizards that change
g or add code
to existing one
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 39
C# 30
The C# Classes 40
C# 30
Parts
All p
parts must use the p
partial keyword
y
All parts must be available at compile time
All parts must have the same accessibility
If any of the parts are declared abstract
The entire type is considered abstract
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 41
C# 30
Parts
Parts can specif
specify different base interfaces
The final type implements all of the interfaces listed
by all of the partial declarations
The C# Classes 42
C# 30
XML comments
Interfaces
Generic-type parameter attributes
Cl
Class
attributes
tt ib t
Members
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 43
C# 30
Example
//MapControl.cs
//MapControl.cs
partialclassMapControl
partialclassMapControl :IMapConsumer
:IMapConsumer
{...}
//M
//MapControl.g.cs
//
M C t l
MapControl.g.cs
partialclassMapControl
partialclassMapControl :UserControl
:UserControl
{...}
//MapControl.Designer.cs
//
MapControl.Designer.cs
[MapComponent]
MapComponent]
partialclassMapControl
partialclass
ti l l
MapControl
M
M C t l {...}
{
}
//Mergedresultingtype:
[MapComponent]
MapComponent]
partialclassMapControl
partialclassMapControl :UserControl
:UserControl,
,IMapConsumer
IMapConsumer
{
}
{...}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 44
C# 30
Partial Methods
Working with
designer-generated
ith designer
generated classes
This suggests
gg
a compile-time
p
mechanism
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 45
C# 30
Partial Methods
//InButton.generated.cs
//InButton
generated cs
publicsealedpartialclassButton{
publicvoidClick(){
OnClicking();
//Dosomework
OnClicked();
}
partialvoidOnClicking();
partialvoidOnClicked();
}
//InButton.mycode.cs
partialclassButton{
partialvoidOnClicking(){
//Mycustomizedwork
}
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 46
C# 30
Best performance:
No virtual call, no derivation, no delegates
Weak customization:
Intra-assembly, compile-time only
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 47
C# 30
Parameter Types
By default
default, method parameters in C# are
passed by value
When the parameter is of a value-type, a local copy of
the arguments value is created for the method
W
Working
ki on this
hi llocall iindependent
d
d
values
l
copy, the
h method
h d
has no influence on the original arguments value
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 48
C# 30
Reference Parameters
C# pro
provides
ides us
s with
ith a special kind of
parameter: reference p
p
parameters
This is the same as passing the address of the
variable in C, or reference in C++
C
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 49
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 50
C# 30
The C# Classes 51
C# 30
Output Parameters
Sometime we need to initialize parameter
in a method
C# does not let us use an uninitialized parameter
Trying to use ref will result a compilation error
Therefore we should use the out keyword
staticDictionary<int,
staticDictionary<int
int,int
int>_cache=
int>
int
>
>_cache=
cache=
newDictionary<int
newDictionary<
int,
,int
int>();
>();
staticvoidMain(string[]args
staticvoidMain(string[]args)
)
{
int value;
_cache.TryGetValue(
cache.TryGetValue(1,out
,out value);
}
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 52
C# 30
Parameters Array
A method can accept a variable number of
parameters
This is the equivalent to the ellipses () in C/C++
Use the params keyword, followed by an array
parameter definition
doubleAverage(params
g (
double[]numbers)...
)
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 53
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 54
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 55
C# 30
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 56
C# 30
The C# Classes 57
C# 30
Chapter 4 Exercise 2
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
The C# Classes 58
C# 30
Chapter Summary
The C# class
C# access modifiers
Class members
Methods,, Properties,
p
, Constructors,, Constant & Readonlyy