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 is the difference between a class and an object in C#?
When you define a class, you define a blueprint or template for a data type. The object is an instance of that class − a concrete implementation created from the blueprint. A class defines the structure and behavior, while objects are the actual entities that hold data and perform actions.
The methods and variables that constitute a class are called members of the class. To access class members, you use the dot (.) operator after the object name. The dot operator links the name of an object with the name of a member.
Class vs Object
| Class | Object |
|---|---|
| A template or blueprint that defines the structure and behavior. | An instance of a class with actual values and memory allocation. |
| No memory is allocated when a class is defined. | Memory is allocated when an object is created. |
| Defined once and can be used to create multiple objects. | Each object is a separate entity with its own data. |
Example: class Car { }
|
Example: Car myCar = new Car();
|
Syntax
Following is the syntax for creating a class and instantiating objects −
// Class definition
class ClassName {
// fields, properties, methods
}
// Object instantiation
ClassName objectName = new ClassName();
Following is the syntax for accessing class members using the dot operator −
objectName.fieldName = value; // Access field objectName.MethodName(); // Call method
Example
using System;
class Box {
private double length;
private double breadth;
private double height;
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) {
// Creating two objects from the same class
Box Box1 = new Box();
Box Box2 = new Box();
double volume;
// Box1 specifications
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box2 specifications
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// Calculate and display volumes
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1: {0}", volume);
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2: {0}", volume);
}
}
The output of the above code is −
Volume of Box1: 210 Volume of Box2: 1560
Using Constructor for Object Initialization
using System;
class Box {
private double length, breadth, height;
public Box(double l, double b, double h) {
length = l;
breadth = b;
height = h;
}
public double getVolume() {
return length * breadth * height;
}
public void displayInfo() {
Console.WriteLine("Dimensions: {0} x {1} x {2}", length, breadth, height);
}
}
class Program {
static void Main(string[] args) {
Box smallBox = new Box(3.0, 4.0, 5.0);
Box largeBox = new Box(10.0, 15.0, 8.0);
smallBox.displayInfo();
Console.WriteLine("Volume: {0}", smallBox.getVolume());
largeBox.displayInfo();
Console.WriteLine("Volume: {0}", largeBox.getVolume());
}
}
The output of the above code is −
Dimensions: 3 x 4 x 5 Volume: 60 Dimensions: 10 x 15 x 8 Volume: 1200
Conclusion
A class is a blueprint that defines the structure and behavior, while an object is a concrete instance of that class with actual data. You can create multiple objects from a single class, each maintaining its own state and capable of performing the defined operations independently.
