Lecture 1 - Classes Objects
Lecture 1 - Classes Objects
www.belgiumcampus.ac.za
Learning Objectives
• Understanding C# Programming
2
www.belgiumcampus.ac.za
1.1 Background
Introduction
3
www.belgiumcampus.ac.za
1.2 Key Terms
In this slide, will discuss some of the key concepts and their practical
implementation in C#.
These include:
• Classes
• Objects
• Methods
4
www.belgiumcampus.ac.za
Classes and Objects
Example#1: Consider an ATM.
Think of it as a class.
5
www.belgiumcampus.ac.za
Example#1: Consider an ATM…
Once you use a class (ATM) and its object (card), now you can perform
operations on the object, like withdrawal of money or checking you
balance or getting statement of your account.
These operations will be methods belonging to that class (ATM) [but you
cannot use them until you create an object out of it.]
6
www.belgiumcampus.ac.za
Defining a Class
7
www.belgiumcampus.ac.za
Example#2: Consider Automobiles
8
www.belgiumcampus.ac.za
Defining Objects
An object is an instance of a
created class.
10
www.belgiumcampus.ac.za
Classes and Objects
11
www.belgiumcampus.ac.za
Creating a Class
Classes are declared by using the keyword class followed by the class name and a
set of class members surrounded by curly braces { }.
12
www.belgiumcampus.ac.za
Class Members
• Classes in C# can have members:
• Data fields, methods, properties, indexers, events, operators,
constructors, destructors, …
• Members can be
• static (common) or specific for a given object
13
www.belgiumcampus.ac.za
Fields and
Methods
Fields:
A field (variable), in C#, is a member of a class or an object of any type that
represents a memory location for storing a value.
E.g int myAge;
Methods:
A method is a group of statements that together perform a task. Every C#
program has at least one class with a method named Main.
To use a method, you need to:
14
www.belgiumcampus.ac.za
Creating a Class in Visual Studio
2017/2019/2022
Example: Namespaces (collection of predefined
classes/elements)
15
www.belgiumcampus.ac.za
Basic syntax for class declaration
In C#, individual classes and class members can be prefixed with access
modifiers.
By default, if you declare a member variable (or anything else) in a class but
do not specify its access level, the member is considered private and
cannot be accessed from outside, i.e. by a non-member of that class.
You can use a mix of public and private members in a class and there is no
rule on which access level should be listed first or last.
16
www.belgiumcampus.ac.za
Creating a class object
We use the new operator when creating an instance of the class
(object).
17
www.belgiumcampus.ac.za
Class and Object Example:
Question:
18
www.belgiumcampus.ac.za
Class and Object Example:
Solution:
19
www.belgiumcampus.ac.za
Class and Object Example:
Solution:
20
www.belgiumcampus.ac.za
Exercise
1. Create a Box class with length and width field members, which will
utilize a calculateArea() method member to print the area of the box. Use
literal values for your fields.
2. Using classes, objects, fields, methods and console input and output,
create a console App that will run as follows:
21
www.belgiumcampus.ac.za
THANK YOU
/belgiumcampusSA
#Belgium Campus
22
www.belgiumcampus.ac.za