You are on page 1of 24

Object-Oriented Programming

• What is OOP?
TOPICS
• Class and Object
• Fields
• Properties
• Methods
• Constructors / Constructor Parameters
• Static keyword
• Using Arrays and Lists
• Using Params keyword
• Passing Value and Reference Type
What is OOP?
• OOP stands for Object-Oriented Programming.
• Procedural programming is about writing
procedures or methods that perform
operations on the data, while object-oriented
programming is about creating objects that
contain both data and methods.
What is OOP?
Object-oriented programming has several advantages over
procedural programming:

• OOP is faster and easier to execute


• OOP provides a clear structure for the programs
• OOP helps to keep the C# code DRY "Don't Repeat
Yourself", and makes the code easier to maintain, modify
and debug
• OOP makes it possible to create full reusable applications
with less code and shorter development time
What is OOP?
Tip: The "Don't Repeat Yourself" (DRY) principle is
about reducing the repetition of code. You should
extract out the codes that are common for the
application, and place them at a single place and
reuse them instead of repeating it.
Class and Object
• Everything in C# is associated with classes and objects,
along with its attributes and methods. For example: in real
life, a car is an object. The car has attributes, such as weight
and color, and methods, such as drive and brake.
• A Class is like an object constructor, or a "blueprint" for
creating objects.
Class
• To create a class, we use the class keyword
• Create a class named “Car” with a variable color:
Object
• An object is created from a class. We have already created the class
named Car, so now we can use this to create objects.
• To create an object of Car, specify
the class name, followed by the
object name, and use the
keyword new:
• Create an object called
“myObj” and use it to
print value of color
Fields
• The variables inside the class called “fields”,
and that you can access them by
creating an object of the class,
and by using the dot syntax (.)
• The following example will create
an object of the Car class, with
the name “myObj”. Then we print
the value of the fields color
and maxSpeed:
Properties
• A property is like a combination of
a variable and a method, and it has
two methods: a get and a set method:
• A private variables can be accesses
within the same class (an outside class
has no access to it). However,
sometimes we need to access them
– and it can be done with properties
Methods
• A method is defined with the name of
the method, followed by parenthesis ().
C# provides some pre-defined methods,
which you already familiar with, such as
Main(), but you can also create your own
methods ti perform certain action:
• To call (execute) a method, write the
method’s name followed by two
parenthesis () and a semicolon:;
Constructors
• A constructor is a special method
that is used to initialize objects.
The advantage of a constructors,
is that it is called when an object
of a class is created. It can be used
to set initial values of fields.
Constructors Parameters
• Constructors can also take
parameters, which is used to
initialize fields
Static keyword
• A static class is non-instantiable i.e. a variable of the class cannot be
created using the new keyword. So, the static class members have to
be accessed using the class name itself.
• A static class is defined using the keyword static. It can only have static
data members and static methods. If this rule is not followed, there is
a compile time error.
Array as parameter
• To use an array as a parameter, we add a square bracket [] after the
parameter’s data type in the method declaration.

• To call this method, we need to declare an array and pass it in as an


argument to the method.
List as parameter
Returning to the method in an array parameter Returning to the method in an list parameter
Params Keyword
• What if you're not sure of the number of arguments? This is where
"params" keyword is useful. 
• The "params" keyword in C# allows a method to accept a variable
number of arguments. C# params works as an array of objects. By
using params keyword in a method argument definition, we can pass a
number of arguments. 
Params Keyword
Value Type vs. Reference Type
• Within the class, we have two methods. The first method accepts a
value type variable and tries to change the value of that variable. It
then prints the value of the variable.
• The second method accepts an array (reference type) and tries to
change the value of the first element in the array. It then prints the
value of that element
Thanks for listening!
Alubijid | Balubal | Cagayan de Oro | Claveria | Jasaan | Oroquieta | Panaon | Villanueva

Home of the Trailblazers

You might also like