Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the differences between class methods and class members in C#?
In C#, understanding the difference between class members and class methods is fundamental to object-oriented programming. Class members are the data components that store the state of an object, while class methods are the functions that operate on that data and define the object's behavior.
Class Members vs Class Methods
| Class Members | Class Methods |
|---|---|
| Store data and represent the state of an object | Define behavior and operations on the object's data |
| Examples: fields, properties, constants | Examples: functions, procedures, constructors |
| Hold values that can change over time | Execute code when called |
| Accessed directly or through properties | Invoked with parentheses and parameters |
Class Members (Data Storage)
Class members are variables that store the state of an object. They are typically declared as private to implement encapsulation, ensuring data is accessed only through controlled methods −
class Box {
private double length; // Class member
private double breadth; // Class member
private double height; // Class member
}
Example
using System;
class Rectangle {
// Class members - store object state
private double length;
private double width;
public void SetDimensions(double len, double wid) {
length = len;
width = wid;
}
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 Program {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.SetDimensions(10, 14);
r.Display();
}
}
The output of the above code is −
Length: 10 Width: 14 Area: 140
Class Methods (Behavior Definition)
Class methods are functions that define what operations can be performed on the object's data. They have access to all class members and can modify the object's state −
public void setLength(double len) { // Class method
length = len; // Modifies class member
}
public double getVolume() { // Class method
return length * breadth * height; // Uses class members
}
Example
using System;
class Box {
// Class members
private double length;
private double breadth;
private double height;
// Class methods
public void SetLength(double len) {
length = len;
}
public void SetBreadth(double bre) {
breadth = bre;
}
public void SetHeight(double hei) {
height = hei;
}
public double GetVolume() {
return length * breadth * height;
}
}
class Program {
static void Main(string[] args) {
Box Box1 = new Box();
Box Box2 = new Box();
// Using class methods to set data
Box1.SetLength(8.0);
Box1.SetBreadth(9.0);
Box1.SetHeight(7.0);
Box2.SetLength(18.0);
Box2.SetBreadth(20.0);
Box2.SetHeight(17.0);
// Using class methods to get results
Console.WriteLine("Volume of Box1: {0}", Box1.GetVolume());
Console.WriteLine("Volume of Box2: {0}", Box2.GetVolume());
}
}
The output of the above code is −
Volume of Box1: 504 Volume of Box2: 6120
How They Work Together
Class members and methods work together to create a cohesive object. Members store the data, while methods provide controlled access and operations on that data. This relationship implements the core principles of object-oriented programming: encapsulation and data hiding.
Conclusion
Class members are the data storage components that maintain an object's state, while class methods are the functions that operate on that data and define the object's behavior. Together, they form the foundation of object-oriented programming in C#, enabling encapsulation and controlled access to an object's internal state.
