- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the scope of a private member variable of a class in C#?
Only functions of the same class can access its private members. Private access specifier allows a class to hide its member variables and member functions from other functions and objects.
Example
using System; namespace RectangleApplication { class Rectangle { //member variables private double length; private double width; public void Acceptdetails() { length = 10; width = 14; } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } //end class Rectangle class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Acceptdetails(); r.Display(); Console.ReadLine(); } } }
Output
Length: 10 Width: 14 Area: 140
Above, the variable length and width are declared private; therefore the methods of the same class can access it.
- Related Articles
- What is the scope of a public member variable of a class in C#?
- What is the scope of a protected member variable of a class in C#?
- What is the scope of a protected internal member variable of a class in C#?
- What is the scope of an internal variable of a class in C#?
- How to initialize const member variable in a C++ class?
- What are the member variables of a class in C#?
- What is the scope of private access modifier in Java?
- What are member functions of a class in C#?
- Explain scope of a variable in C language.
- What is the default access for a class member in C#?
- Explain python namespace and scope of a variable.
- Are the private variables and private methods of a parent class inherited by the child class in Java?
- How to access a derived class member variable by an interface object in Java?
- Can private methods of a class be accessed from outside of a class in Java?
- How to access the private methods of a class from outside of the class in Java?

Advertisements