Class, Object, & Constructors
1. Description
A class is a blueprint for creating objects. It defines a set of properties (data) and methods (behaviors) that objects of that type will have. An object is an instance of a class, a concrete entity created from the blueprint. A constructor is a special method in a class that is called when a new object is created, used to initialize the object's properties.
2. Why It Is Important
Classes and objects are the fundamental concepts of Object-Oriented Programming (OOP). They allow you to model real-world entities, organize your code into logical units, and create reusable and maintainable code. Constructors ensure that objects are created in a valid and predictable state.
3. Real-World Examples
- A
Carclass can define properties likeColor,Model, andSpeed, and methods likeAccelerate()andBrake(). Each individual car is an object of theCarclass. - A
Userclass in a social media application could have properties likeUsername,Email, and a list ofPosts. - When creating a new
Userobject, a constructor would ensure that theUsernameandEmailare provided and are not empty.
4. Syntax & Explanation
using System;
// Defining a class named 'Customer'
class Customer
{
// Properties
public string Name { get; }
public int Age { get; set; }
// Constructor
public Customer(string name, int age)
{
// Basic validation in the constructor
if (string.IsNullOrWhiteSpace(name))
{
// Throw an exception if the name is not valid
throw new ArgumentException("Name cannot be empty", nameof(name));
}
Name = name;
Age = age;
}
// Method
public void Print()
{
Console.WriteLine($"Customer: {Name}, Age: {Age}");
}
}
class Program
{
static void Main()
{
// Creating an object of the Customer class
var customer1 = new Customer("Sam", 30);
customer1.Print(); // Output: Customer: Sam, Age: 30
// Creating another object
var customer2 = new Customer("Alice", 25);
customer2.Age = 26; // Modifying a property
customer2.Print(); // Output: Customer: Alice, Age: 26
}
}
5. Use Cases
- Modeling real-world entities in your application, such as
Product,Order,Employee, etc. - Encapsulating data and behavior into a single unit.
- Creating complex data structures.
- Ensuring that objects are always created in a valid state using constructors.
6. Mini Practice Task
- Create a
Productclass with properties forName(string) andPrice(decimal). - Add a constructor to the
Productclass that accepts the name and price as parameters and initializes the properties. - In the constructor, add a validation to ensure that the
Priceis not negative. If it is, throw anArgumentException. - Create a few instances of the
Productclass and print their details to the console.