SlideShare a Scribd company logo
Learn C# Programming
Encapsulation & Methods
Eng Teong Cheah
Microsoft MVP in Visual Studio &
Development Technologies
Agenda
•Encapsulation
•Methods
C# - Encapsulation
C# - Encapsulation
Encapsulation is defined ‘as the process of enclosing one or more
items within a physical or logical package’. Encapsulation, in object
oriented programming methodology, prevents access to
implementation details.
Abstraction and encapsulation are related features in object oriented
programming. Abstraction allows making relevant information visible
and encapsulation enables a programmer to implement the desired
level of abstraction.
C# - Encapsulation
Encapsulation is implemented by using access specifiers. An access
specifier defines the scope and visibility of a class member. C#
supports the following access specifiers:
- Public
- Private
- Protected
- Internal
- Protected internal
C# - Encapsulation
Public Access Specifier
Public access specifier allows a class to expose its member variables
and member functions to other functions and objects. Any public
member can be accessed from outside the class.
Demo
C# - Encapsulation
Priavte Access Specifier
Private access specifier allows a class to hide its member variables
and member functions from other functions and objects. Only
functions of the same class can access its private members. Even an
instance of a class cannot access its private members.
Demo
C# - Encapsulation
Protected Access Specifier
Protected access specifier allows a child class to access the member
variables and member functions of its base class. This way it helps in
implementing inheritance.
C# - Encapsulation
Internal Access Specifier
Internal access specifier allows a class to expose its member variables
and member functions to other functions and objects in the current
assembly. In other words, any member with internal access specifier
can be accessed from any class or method defined within the
application in which the member is defined.
Demo
C# - Encapsulation
Protected Internal Access Specifier
The protected internal access specifier allows a class to hide its
member variables and member functions from other class objects
and functions, except a child class within the same application. This is
also used while implementing inheritance.
C# - Methods
C# - Methods
A method is a group of statements that together perform a task.
Every C# program has at least one class with a method name Main.
To use a method, you need to:
- Define the method
- Call the method
C# - Methods
Defining Methods in C#
When you define a method, you basically declare the elements of it
structure. The syntax for defining a method in C# is as follows:
<Access Specifier> <Return Type> <Method Name>(Parameter List)
{
Method Body
}
C# - Methods
Following are the various elements of a method:
- Access Specifier: This determines the visibility of a variable or a
method from another class.
- Return type: A method may return a value. The return type is the
data type of the value the method returns. If the method is not
returning any values, then the return type is void.
- Method name: Method name is a unique identifier and it is case
sensitive. It cannot be same as any other identifier declared in the
class.
C# - Methods
- Parameter list: Enclosed between parantheses, the parameters are
used to pass and receive data from a method. The parameter list
refers to the type, order, and number of the parameters of a
method. Parameters are optional; that is, a method may contain no
parameters.
- Method body: This contains the set of instructions needed to
complete the required activity.
C# - Methods
Example
Following code snippet shows a function FindMax that takes two
integer values and returns the larger of the two. It has public access
specifier, so it can be accessed from outside the class using an
instance of the class.
C# - Methods
class NumberManipulator
{
public int FindMax(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
...
}
Demo: Calling Methods in
C#
Demo:Recursive Method
Call
C# - Passing Parameters
to a Method
C# -Passing Parameters to a Method
When method with parameters is called, you need to pass the
parameters to the method. There are three ways that parameters can
be passed to a method:
- Value parameters
- Reference parameters
- Output parameters
C# -Passing Parameters to a Method
Value parameters
This is the default mechanism for passing parameters to a method. In
this mechanism, when a method is called, a new storage location is
created for each value parameter.
The values of the actual parameters are copied into them. Hence, the
changes made to the parameter inside the method have no effect on
the argument.
Demo: Value parameters
C# -Passing Parameters to a Method
Reference parameters
A reference parameter is a reference to a memory location of a
variable. When you pass parameters by reference, unlike value
parameters, a new storage location is not created for these
parameters. The reference parameters represent the same memory
location as the actual parameters that are supplied to the method.
Demo: Reference
parameters
C# -Passing Parameters to a Method
Output parameters
A return statements can be used for returning only one value from a
function. However, using output parameters, you can return two
values from a function. Output parameters are similar to reference
parameters, excepts that they transfer data out of the method rather
than into it.
Demo: Output parameters
Related Content
•TutorialsPoint
www.tutorialspoint.com
Thank You

More Related Content

PPTX
PL/SQL - CURSORS
IshaRana14
 
PPT
Python Dictionaries and Sets
Nicole Ryan
 
PPTX
Arrays in Java
Abhilash Nair
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
Muhammad Hammad Waseem
 
PPTX
CSharp Presentation
Vishwa Mohan
 
PPTX
Array in c#
Prem Kumar Badri
 
PPTX
String Handling in c++
Fahim Adil
 
PL/SQL - CURSORS
IshaRana14
 
Python Dictionaries and Sets
Nicole Ryan
 
Arrays in Java
Abhilash Nair
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
[OOP - Lec 08] Encapsulation (Information Hiding)
Muhammad Hammad Waseem
 
CSharp Presentation
Vishwa Mohan
 
Array in c#
Prem Kumar Badri
 
String Handling in c++
Fahim Adil
 

What's hot (20)

PPT
Data structure lecture 1
Kumar
 
PDF
Class and Objects in Java
Spotle.ai
 
PPT
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
PPTX
OOPS Basics With Example
Thooyavan Venkatachalam
 
PPTX
Compiler design syntax analysis
Richa Sharma
 
PDF
Object oriented approach in python programming
Srinivas Narasegouda
 
PPT
Java oops PPT
kishu0005
 
PPTX
Method Overloading in Java
Sonya Akter Rupa
 
PDF
Methods in Java
Jussi Pohjolainen
 
PPT
Collection Framework in java
CPD INDIA
 
PPTX
Functions in python slide share
Devashish Kumar
 
PPTX
Java string handling
Salman Khan
 
PPTX
C# in depth
Arnon Axelrod
 
PPT
C# Basics
Sunil OS
 
PPTX
Unit I - Evaluation of expression
DrkhanchanaR
 
PPTX
Pass by value and pass by reference
TurnToTech
 
PDF
Wrapper classes
Ravi_Kant_Sahu
 
PPTX
LL(1) parsing
KHYATI PATEL
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
Data structure lecture 1
Kumar
 
Class and Objects in Java
Spotle.ai
 
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
OOPS Basics With Example
Thooyavan Venkatachalam
 
Compiler design syntax analysis
Richa Sharma
 
Object oriented approach in python programming
Srinivas Narasegouda
 
Java oops PPT
kishu0005
 
Method Overloading in Java
Sonya Akter Rupa
 
Methods in Java
Jussi Pohjolainen
 
Collection Framework in java
CPD INDIA
 
Functions in python slide share
Devashish Kumar
 
Java string handling
Salman Khan
 
C# in depth
Arnon Axelrod
 
C# Basics
Sunil OS
 
Unit I - Evaluation of expression
DrkhanchanaR
 
Pass by value and pass by reference
TurnToTech
 
Wrapper classes
Ravi_Kant_Sahu
 
LL(1) parsing
KHYATI PATEL
 
07. Virtual Functions
Haresh Jaiswal
 
Ad

Similar to Learn C# Programming - Encapsulation & Methods (20)

PPTX
14method in c#
Sireesh K
 
PPTX
Notes(1).pptx
InfinityWorld3
 
PPT
Constructor
abhay singh
 
PPTX
Unit2_1.pptx Introduction to C# Language features
Priyanka Jadhav
 
PPTX
Unit 1:DOT NET Framework CLR(Common Language Runtime )
Priyanka Jadhav
 
PDF
static methods
Micheal Ogundero
 
PPTX
Methods In C-Sharp (C#)
Abid Kohistani
 
PDF
Xamarin: C# Methods
Eng Teong Cheah
 
PDF
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 
PPT
Methods in C#
Prasanna Kumar SM
 
PPTX
Intro to C# - part 2.pptx emerging technology
worldchannel
 
PDF
overview of c#
Kandreo Gotro
 
DOCX
csharp.docx
LenchoMamudeBaro
 
PPT
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
DOCX
C# concepts
lexilijoseph
 
PPTX
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 
PPTX
METHODS OR FUNCTIONS IN C for dotnet.pptx
ArjunKhanal8
 
PPTX
27c
Sireesh K
 
PPTX
27csharp
Sireesh K
 
PDF
C# chap 4
Shehrevar Davierwala
 
14method in c#
Sireesh K
 
Notes(1).pptx
InfinityWorld3
 
Constructor
abhay singh
 
Unit2_1.pptx Introduction to C# Language features
Priyanka Jadhav
 
Unit 1:DOT NET Framework CLR(Common Language Runtime )
Priyanka Jadhav
 
static methods
Micheal Ogundero
 
Methods In C-Sharp (C#)
Abid Kohistani
 
Xamarin: C# Methods
Eng Teong Cheah
 
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 
Methods in C#
Prasanna Kumar SM
 
Intro to C# - part 2.pptx emerging technology
worldchannel
 
overview of c#
Kandreo Gotro
 
csharp.docx
LenchoMamudeBaro
 
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
C# concepts
lexilijoseph
 
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
ArjunKhanal8
 
27csharp
Sireesh K
 
Ad

More from Eng Teong Cheah (20)

PDF
Modern Cross-Platform Apps with .NET MAUI
Eng Teong Cheah
 
PDF
Efficiently Removing Duplicates from a Sorted Array
Eng Teong Cheah
 
PDF
Monitoring Models
Eng Teong Cheah
 
PDF
Responsible Machine Learning
Eng Teong Cheah
 
PDF
Training Optimal Models
Eng Teong Cheah
 
PDF
Deploying Models
Eng Teong Cheah
 
PDF
Machine Learning Workflows
Eng Teong Cheah
 
PDF
Working with Compute
Eng Teong Cheah
 
PDF
Working with Data
Eng Teong Cheah
 
PDF
Experiments & TrainingModels
Eng Teong Cheah
 
PDF
Automated Machine Learning
Eng Teong Cheah
 
PDF
Getting Started with Azure Machine Learning
Eng Teong Cheah
 
PDF
Hacking Containers - Container Storage
Eng Teong Cheah
 
PDF
Hacking Containers - Looking at Cgroups
Eng Teong Cheah
 
PDF
Hacking Containers - Linux Containers
Eng Teong Cheah
 
PDF
Data Security - Storage Security
Eng Teong Cheah
 
PDF
Application Security- App security
Eng Teong Cheah
 
PDF
Application Security - Key Vault
Eng Teong Cheah
 
PDF
Compute Security - Container Security
Eng Teong Cheah
 
PDF
Compute Security - Host Security
Eng Teong Cheah
 
Modern Cross-Platform Apps with .NET MAUI
Eng Teong Cheah
 
Efficiently Removing Duplicates from a Sorted Array
Eng Teong Cheah
 
Monitoring Models
Eng Teong Cheah
 
Responsible Machine Learning
Eng Teong Cheah
 
Training Optimal Models
Eng Teong Cheah
 
Deploying Models
Eng Teong Cheah
 
Machine Learning Workflows
Eng Teong Cheah
 
Working with Compute
Eng Teong Cheah
 
Working with Data
Eng Teong Cheah
 
Experiments & TrainingModels
Eng Teong Cheah
 
Automated Machine Learning
Eng Teong Cheah
 
Getting Started with Azure Machine Learning
Eng Teong Cheah
 
Hacking Containers - Container Storage
Eng Teong Cheah
 
Hacking Containers - Looking at Cgroups
Eng Teong Cheah
 
Hacking Containers - Linux Containers
Eng Teong Cheah
 
Data Security - Storage Security
Eng Teong Cheah
 
Application Security- App security
Eng Teong Cheah
 
Application Security - Key Vault
Eng Teong Cheah
 
Compute Security - Container Security
Eng Teong Cheah
 
Compute Security - Host Security
Eng Teong Cheah
 

Recently uploaded (20)

PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Software Development Methodologies in 2025
KodekX
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Doc9.....................................
SofiaCollazos
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Software Development Methodologies in 2025
KodekX
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
Architecture of the Future (09152021)
EdwardMeyman
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Doc9.....................................
SofiaCollazos
 

Learn C# Programming - Encapsulation & Methods

  • 1. Learn C# Programming Encapsulation & Methods Eng Teong Cheah Microsoft MVP in Visual Studio & Development Technologies
  • 4. C# - Encapsulation Encapsulation is defined ‘as the process of enclosing one or more items within a physical or logical package’. Encapsulation, in object oriented programming methodology, prevents access to implementation details. Abstraction and encapsulation are related features in object oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction.
  • 5. C# - Encapsulation Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers: - Public - Private - Protected - Internal - Protected internal
  • 6. C# - Encapsulation Public Access Specifier Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.
  • 8. C# - Encapsulation Priavte Access Specifier Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.
  • 10. C# - Encapsulation Protected Access Specifier Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance.
  • 11. C# - Encapsulation Internal Access Specifier Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.
  • 12. Demo
  • 13. C# - Encapsulation Protected Internal Access Specifier The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance.
  • 15. C# - Methods A method is a group of statements that together perform a task. Every C# program has at least one class with a method name Main. To use a method, you need to: - Define the method - Call the method
  • 16. C# - Methods Defining Methods in C# When you define a method, you basically declare the elements of it structure. The syntax for defining a method in C# is as follows: <Access Specifier> <Return Type> <Method Name>(Parameter List) { Method Body }
  • 17. C# - Methods Following are the various elements of a method: - Access Specifier: This determines the visibility of a variable or a method from another class. - Return type: A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void. - Method name: Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.
  • 18. C# - Methods - Parameter list: Enclosed between parantheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters. - Method body: This contains the set of instructions needed to complete the required activity.
  • 19. C# - Methods Example Following code snippet shows a function FindMax that takes two integer values and returns the larger of the two. It has public access specifier, so it can be accessed from outside the class using an instance of the class.
  • 20. C# - Methods class NumberManipulator { public int FindMax(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } ... }
  • 23. C# - Passing Parameters to a Method
  • 24. C# -Passing Parameters to a Method When method with parameters is called, you need to pass the parameters to the method. There are three ways that parameters can be passed to a method: - Value parameters - Reference parameters - Output parameters
  • 25. C# -Passing Parameters to a Method Value parameters This is the default mechanism for passing parameters to a method. In this mechanism, when a method is called, a new storage location is created for each value parameter. The values of the actual parameters are copied into them. Hence, the changes made to the parameter inside the method have no effect on the argument.
  • 27. C# -Passing Parameters to a Method Reference parameters A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters. The reference parameters represent the same memory location as the actual parameters that are supplied to the method.
  • 29. C# -Passing Parameters to a Method Output parameters A return statements can be used for returning only one value from a function. However, using output parameters, you can return two values from a function. Output parameters are similar to reference parameters, excepts that they transfer data out of the method rather than into it.