- 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 public member variable of a class in C#?
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.
In the below example the variables length and width have been declared public. Now you can even access them outside the Main() method.
The variables are accessed using the instance of the class.
Rectangle r = new Rectangle(); r.length = 4.5; r.width = 3.5;
Let us see the complete code.
Example
Using System; namespace RectangleApplication { class Rectangle { // member variables public double length; public double width; 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.length = 4.5; r.width = 3.5; r.Display(); Console.ReadLine(); } } }
- Related Articles
- What is the scope of a private 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 is the scope of public access modifier in Java?
- What are the member variables of a class in C#?
- 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.
- How to access a derived class member variable by an interface object in Java?
- What is the lifetime of a static variable in a C++ function?
- What is the use of scope resolution operator in C++?
- PHP Variable Scope

Advertisements