You are on page 1of 56

OOPROGR

C#
Programming
LESSON 5-1

ROMMEL L. DORIN
Contents

Methods

Classes and Object

Constructor
Methods in C#

In C#, Method is a separate code block,


and that contains a series of statements
to perform particular operations.
Methods in C#

• In C#, Method is a separate code block, and


that contains a series of statements to perform
particular operations.
• Methods must be declared either in class or
struct by specifying the required parameters.
• Generally, methods are useful to improve code
reusability by reducing code duplication. If we
have the same functionality to perform in
multiple places, then we can create one
method with the required functionality and use
it wherever it is required in the application.
Methods in C#

• Syntax:
Methods in C#
• Access_Specifier - It is used to define an access level,
either public or private, etc., to allow other classes to access
the method. If we didn’t mention any access modifier, then
by default, it is private.
• Return_Type - It is used to specify the type of value the
method can return. If the method is not returning any value,
then we need to mention void as the return type.
• Method_Name - It must be a unique name to identify the
method in a class.
• Parameters - The method parameters are useful to send or
receive data from a method, and these method parameters
are enclosed within parentheses and are separated by
commas. If no parameters are required for a method, we
need to define a method with empty parentheses.
Methods in C#
In C#, there are 4 basic types of access modifiers.

• public - a type or type member public, it can be accessed


from anywhere.
• private - a type member with the private access modifier, it
can only be accessed within the same class or struct.
• protected - a type member as protected, it can only be
accessed from the same class and its derived classes.
• internal - a type or type member as internal, it can be
accessed only within the same assembly.

An assembly is a collection of types (classes, interfaces, etc)


and resources (data). They are built to work together and form
a logical unit of functionality.
Level of Accessibility
Methods in C#

• Example:
Methods in C#

• Example:
C# Static Methods

In C#, if we create methods with static, then we can


directly invoke those methods from the class level
without creating an object.

If you observe the above example, the Main method


is static, and we are invoking
the GetUserDetails method by creating an instance of
the Program class. Instead, if we create a static
method, we can directly access those methods in
the Main() method without creating an instance of an
object.
C# Static Methods
Methods in C#

2 Types of methods:

a. Built in – stored in the library


b. Programmer defined – custom made
by developers

2 types of Programmer defined methods:


a. Void
b. Value returning
Methods in C#

2 Types of methods:

a. Built in – stored in the library


b. Programmer defined – custom made
by developers

2 types of Programmer defined methods:


a. Void
b. Value returning
C# Methods
C# Methods
C# Methods
C# Methods
OOP Concept in C#

Object oriented programming or OOPS


concept in C# is a type of programming
that contains the collection of objects.
Each Object contains data fields and
data members. Unlike procedural
programming languages like C,
FORTRAN, BASIC, etc., C# Object
oriented programming languages can
easily upgrade.
Programming Paradigm
Programming Paradigm
Programming Paradigm
Programming Paradigm
Programming Paradigm
Programming Paradigm
Programming Paradigm
C# Class

• A class is a user-defined type, and it is


the main concept of the C# OOPS or
object oriented programming.
• A Class is just like a blueprint or a
template for creating an object. The
C# class doesn’t have any memory
allocated. But the memory is allocated
only for the created class instance.
C# Class
C# Class
C# Object

• The C# Object is said to be the


instance of a class, and Object holds
data fields, member functions(data
members).
• Actually, in a real-time environment,
everything is said to be an object. The
same is applicable in this C# object
oriented programming.
C# Object

• An object is an entity that can be seen


or imagined.
• The Object has some properties for
identifying its current state and for the
sake of validations.
• Data members or member functions
for telling their functions or behavior.
• Events for representing the change of
its state.
C# Object

• A nob surrounding by
blades, its brand, the
color of it depicts the
state of the revolving
fan.
• The airflow created by
the fan is said to be its
functionality.
C# Object

• Whereas change in
the position of blades
while revolving
represents the change
of its state.
C# Object
C# Object
• We use the name of
objects along with the
. (dot) operator to
access members of a
class.
C# Object
• Creating multiple
objects from the same
class
C# Object
• Creating objects in a
different class
C# Class Instance Syntax

• Syntax:
• Differences among C#
Variable, Instance (Object)
and Reference
• The C# Variable is nothing but a copy
of a class which is not initialized.

• E.g. int x //x is a copy of type int


string s // String is a class and s is
a copy of the class string
C# Class Instance

• Data type int and the class string is a


blueprint that doesn’t have any
memory allocated to it. But for the
variables x and s, memory is allocated.

• Variable is a copy of a particular data


type that holds some memory. In
contrast, the C# datatype is a logical
one or a blueprint for which no memory
allocated.
C# Class Instance

• A house plan is just like the datatype,


and the physical house constructed
based on that plan is the variable. i.e.,
int or string in the above context
doesn’t have any memory whereas a
variable has physical memory.
C# Class Instance
C# Class Instance
C# Instance
• The C# instance in OOPS concept is
nothing but a copy of the class, which
helps initialize a variable using a keyword
new. Every instance has its own memory.

• The memory allocated for one C#


instance never shares with another
instance. It means how many instances
created; the memory allocations allocated
for that many instances without memory
sharing.
C# Instance
C# Reference
• The Reference in C# is also a copy of the class,
which initializes along with an instance that
already existed. Still, a reference doesn’t have
any memory allocated. Instead, it created for
sharing the memory of the instance.
• A reference to a C# class can also say as a
pointer to the instance. Every action performed
on the fields and member functions using the
instance a reference pointed to reflects that
Reference. And vice versa (changes made
using Reference reflects the instance to which it
created).
C# Reference
C# Constructor

In C#, a constructor is similar to a method


that is invoked when an object of the
class is created. However, unlike
methods, a constructor:

• has the same name as that of the


class
• does not have any return type
C# Constructor

Creating C# constructor
C# Constructor

In C#, a constructor is called when we try


to create an object of a class.
C# Constructor

Types of constructors:
• Parameterless Constructor
• Parameterized Constructor
• Default Constructor
C# Constructor
Parameterless Constructor – a
constructor without parameters.
C# Constructor
Parameterized Constructor – a
constructor can accept parameters.
C# Constructor
Parameterized Constructor – a
constructor can accept parameters.

• The values passed to


the constructor are
called arguments. We
must pass the same
number and type of
values as parameters.
C# Constructor
Default Constructor – If we have not defined
a constructor in our class, then the C# will
automatically create a default constructor with
an empty code and no parameters.

You might also like