🧱 What is a Class in C#?
A class is one of the fundamental building blocks of Object-Oriented Programming (OOP).
📌 Definition
A class is a user-defined data type.
It is a collection of data members (variables) and member functions (methods).
🔹 Data Members
These are the variables declared inside a class.
✅ Example
int a;
string s;
🔹 Member Functions
These are the methods defined inside a class.
✅ Example
void display()
{
}
⚙️ Key Points About Class
A class acts as a blueprint for creating objects.
It contains:
Data (variables)
Behavior (methods)
Classes help in organizing code using encapsulation.
🔐 Access Specifier
A class is generally declared as public so it can be accessed from anywhere.
However, it can also be private, internal, or protected depending on requirement.
🧱 Creating an Instance of a Class
To use a class, we create an instance (object) using the new keyword.
✅ Example
using System;
class Program
{
static void Main()
{
Program p1 = new Program();
// Program → class
// p1 → instance of the class
}
}
🧠 Key Takeaways
Class = Blueprint
Object/Instance = Actual implementation
Data members → store data
Member functions → define behavior
newkeyword is used to create an instance
This concept is the foundation of OOP and is essential for understanding advanced topics like inheritance, polymorphism, and abstraction.
Comments
Post a Comment