

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How is encapsulation implemented in C#?
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, etc.
Encapsulation can be understood by taking an example of private access specifier that allows a class to hide its member variables and member functions from other functions and objects.
In the following example we have length and width as variables assigned private access specifier −
Example
using System; namespace RectangleApplication { class Rectangle { private double length; private double width; public void Acceptdetails() { length = 20; width = 30; } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Acceptdetails(); r.Display(); Console.ReadLine(); } } }
- Related Questions & Answers
- How optimized Bitmap Filtering is implemented?
- What is encapsulation in Java?
- What is encapsulation in C#?
- How multiple inheritance is implemented using interfaces in Java?
- What is Generic Routing Encapsulation (GRE)?
- Encapsulation in Java
- Encapsulation in C#
- Encapsulation in C++
- How Does Encapsulation Work in Ruby?
- Explain Encapsulation in PHP.
- How is IPC implemented in the Android, MAC, Windows Operating systems?
- How are virtual functions implemented in C++?
- Why is sizeof() implemented as an operator in C++?
- Abstraction vs Encapsulation in Java
- Advantages of encapsulation in Java
Advertisements