You are on page 1of 33

Contents

1 Namespace............................................................................................................................1
19 Classes...............................................................................................................................10
20 – Static and instance class members.................................................................................12
21 Inheritance........................................................................................................................14
22 Method hiding...................................................................................................................18
23 Polymorphism...................................................................................................................20
Method overloading...............................................................................................................22
26 Properties / encapsulation................................................................................................22
27 Properties..........................................................................................................................23
28 Structs...................................................................................................................................24
29 Classes vs Structs...............................................................................................................25
30 Introduction to interfaces.................................................................................................28
31 Explicit interfaces implementation....................................................................................29
32 Abstract classes.................................................................................................................30

1 Namespace
3-4

6
7

Mas eh melhor usar convert pois dá exceção se não conseguir converter enquanto que ()
converte para o menor valor possível:

8
9

10
11
12

13

14
15

16

 Static method não precisa criar instancia. Instance methods precisa.

Instance method:
Static method:

Parameters
17

Reference:

Out parameters
Parameters with params keyword makes parameters optional:

Without params keyword it is a value parameter, mandatory

Passando array
Params array sempre no final

Só pode uma Params array

18
19 Classes
Constructor, destructor and how to create class instance:

 Constructors are rated automatically when we create an instance of the class

It is possible to have two constructors:

Default constructor in case only no constructor was provided. If a constructor was provided
then the default one does not work:
Using two constructors:

20 – Static and instance class members


Static field never changes, and it is only one for all the instances of the classes.
Static members are not related to the object, which is an instance of the class, but directly to
the class
Static constructors do not allow access modifiers and are used to initialize static fields

21 Inheritance
Multiple base classes is not supported

Class A has all the elements from class Employee and PartTimeEmployee
22 Method hiding

Using method from parent class in child class using base keyword:
The way of calling the hidden method is:

Another was is using casting:


23 Polymorphism

Even though the specialized methods from child classes hide the corresponding methods from
parent class, given that the object type is of parent class, the parent class method is called.

In order to get the results from the specialized methods from the child classes, use virtual
keyword in the method from parent class what will make it possible to override this method in
child classes:
The result is then
Method overloading

26 Properties / encapsulation
27 Properties

The property can have any name, but to make it simple give it the name of the field:
The syntax is like a public field:

28 Structs
29 Classes vs Structs
In the following example, both C1 and C2 will have the same name (i.e. Marry) as a class is a
reference type. While I and j which are value types will have different values (i=10, j=11).
30 Introduction to interfaces

The implementation of the interface methods is mandatory and the implanted methods must
have the same name and signature, otherwise a compile error will be issued.

Simple example of interface:


Interfaces make it very flexible the inheritance framework:

31 Explicit interfaces implementation


In case two interfaces give the same name to a method:
How to use both methods:

32 Abstract classes
The main point is that an abstract class is like any other class, but it is incomplete (i.e. it may
contain abstract members, it does not contain fields), whence it cannot be instantiated.
Basic implementation:

An abstract class can have abstract and non-abstract members:

Given an abstract class can have non-abstract


members, than the following is possible:

33 Enums

More readable: instead of using numbers, use


enum
34 Properties vs. Fields

You might also like