- 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 are the member variables of a class in C#?
A class is a blueprint that has member variables and functions in C#. This describes the behavior of an object.
Let us see the syntax of a class to learn what are member variables −
<access specifier> class class_name { // member variables <access specifier> <data type> variable1; <access specifier> <data type> variable2; ... <access specifier> <data type> variableN; // member methods <access specifier> <return type> method1(parameter_list) { // method body } <access specifier> <return type> method2(parameter_list) { // method body } ... <access specifier> <return type> methodN(parameter_list) { // method body } }
Member variables are the attributes of an object (from design perspective) and they are kept private to implement encapsulation. These variables can only be accessed using the public member functions.
Below length and width are member variables because a new instance/ of this variable will get created for each new instance of Rectangle class.
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
- Related Articles
- What are member functions of a class in C#?
- What is the scope of a public member variable of a class in C#?
- 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 default access for a class member in C#?
- What is the scope of a protected internal member variable of a class in C#?
- Member variables in Java
- What are class variables, instance variables and local variables in Java?
- What are the types of variables a class can have in Java?
- Member variables vs Local variables in Java
- C++ static member variables and their initialization
- How static variables in member functions work in C++?
- What are static member functions in C#?
- What are variables and types of variables in C++?
- How to initialize const member variable in a C++ class?

Advertisements