0% found this document useful (0 votes)
3 views

OOP Classes

The document provides an overview of class members in C#, focusing on fields, properties, constructors, and the distinction between static and common classes. It emphasizes best practices such as using private fields, readonly and const keywords, encapsulation, and the use of properties for getter and setter methods. Additionally, it discusses the purpose of static constructors and the singleton pattern for managing class instances.

Uploaded by

CHAI KO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

OOP Classes

The document provides an overview of class members in C#, focusing on fields, properties, constructors, and the distinction between static and common classes. It emphasizes best practices such as using private fields, readonly and const keywords, encapsulation, and the use of properties for getter and setter methods. Additionally, it discusses the purpose of static constructors and the singleton pattern for managing class instances.

Uploaded by

CHAI KO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Classes and class members

Short theory
Class Members - Fields

Note: Private fields usually mark with


underscore before filed name (_) but it is not
necessary.

Good Practice: Each field is private by default


if we are not using “private” keyword.
However it is a good practice to put the
“private” keyword before the field in the case
when field is meant to be private.
Class Members - Fields

Note: “readonly” keyword allows to change


the value of the field ONLY inside the
constructor

Good Practice: It is a good practice to mark


field as “readonly” if it is really meant to be
readonly by the business logic and do not miss
this keyword.
Class Members - Fields

Note: “const” keyword does not allow to


change the field value anywhere within the
code and it must be be initialized

Good Practice: It is a good practice to


name all constants in capitalized letters
(example: NAME instead of _name)
Class Members - Fields

Bad Practice: It is a bad practice to expose fields (make them public (or internal)
= make them accessible everywhere or everywhere within the module).
It is violates the Encapsulation OOP-principle in the following aspects:
- No Abstraction - Public fields expose the internal implementation details,
breaking this abstraction.
- Limited Flexibility - Directly exposing fields limits your ability to evolve the
class without breaking existing code.
- No Compatibility with Interfaces - Interfaces in C# cannot have fields
Class Members - Fields

Good practice: One of the good practices could be passing


parameters through the constructor and setting the field
values inside the constructor.
Class Members - Fields

Good practice: Another good practice – is to encapsulate (hide)


logic inside methods that will handle getting and setting the
value of the fields (SetName, GetName methods on pictures)
Class Members - Properties

Instead of using Get and Set methods – we can use built-in feature in C# which is a syntactic sugar for those methods –
it’s called ”properties”

Equivalents to
Class Members - Properties

Good practice: If you don’t have any other logic that should be performed in the getter and setter in properties – then
we can simplify it to the following short form:

And call like this:

Or like this:

Good practice: If your properties will not be changed in any other code and will be initialized immediately after creating
the new instance of class – you should use “init” instead of “set”. This feature available since C# 9.0 (2020).

Will not work:

Works:
Class Members – Constructors

Constructors – are methods that are


executed when the class instance is created
and have the same name as a class.

If no constructor is implemented – default


empty parameterless constructor will be
used.
Class Members – Constructors

Constructors similar to methods can be overloaded – they have the same name but different set of parameters.

: this(…) – means that we


call the existing
constructor in this class
Class Members – Constructors

Constructors can be private. They will be useful for example when we need to have only single instance of our class (singleton pattern).
Use cases: For example we have the class that manages the database connection. We should instantiate the connection only once and
prevent doing this more times.

le ak
r y
e mo ssible
s to m of po
d t
Lea to a lo ctions
e
due conn
Class Members – Constructors

Fix: Use private constructor to avoid creating instance of class by default and delegate the instance creation to custom
static property or method. If instance is already created – this property / method will return it instead of creating a
new one (Singleton pattern).

This does not work anymore – we prevent creating a new


instances – so prevent a new several connections

So now we should call instance in the following way:


Class – Static classes vs Common classes

Program execution started

Main method start execution

Instance of class Student is created


and placed in memory (heap)

Using the created instance, some


logic is being performed

GC cleans up for class Student


when it becomes unused

Program stops
Class – Static classes vs Common classes

Good practice: Use static classes only if they


needed. They should not keep and handle a very
Program execution started
big amount of data because they are initialized
after program starts and remain in heap till
program ends.
Instance of StudentStatic class
Use cases: Util or helper classes like Math that automatically created and placed in
provide helper methods or extensions memory (heap)

Main method start execution

Using the created instance, some


logic is being performed

Program stops
Class – Static constructor

Static constructors are executed when any class member of class is called. Each class can contain only one static constructor.
Use case: If we want to initialize our logic only once during the application lifetime.
Example: We have a logger and a class that is used to handle logger logic. We want our logger to be configured only once.
Class – Static constructor

If we make our class static – static constructor will be executed when the “Error” method is called

You might also like