1Building Better Classes in C#
Hi friends, in this article we will learn about constructors, properties and access modifiers in C#. A constructor is a special method that runs when an object is created. Properties control how we read and update data. Access modifiers define where classes and their members can be accessed. These concepts help us build secure, reusable and well-organised applications. They are widely used in enterprise applications, Web APIs and object-oriented programming.
2Think About a Bank Account
Imagine creating a bank account for a customer. When the account is created, we need to initialise the customer's name and account number. This is where a constructor helps.
We should also prevent other parts of the application from directly changing sensitive account information. Properties and access modifiers help us control how that data is accessed and updated.
3Understand These Three Concepts
1. Constructors
A constructor has the same name as its class and no return type. It runs automatically when we create an object using new.
2. Properties
Properties provide controlled access to class data using get and set. We can also use private set to restrict updates from outside the class.
3. Access Modifiers
Access modifiers control visibility and accessibility of classes and members.
| Modifier | Accessibility |
|---|---|
| public | Accessible wherever the containing type is accessible |
| private | Only inside the containing type |
| protected | Inside the type and its derived types |
| internal | Within the same assembly |
4Build a Bank Account Class
Let's create a class with a constructor, properties and a private method. The balance can be updated only through the Deposit method.
using System;
class BankAccount
{
public string AccountHolder { get; }
public decimal Balance { get; private set; }
public BankAccount(string name)
{
AccountHolder = name;
Balance = 0;
}
public void Deposit(decimal amount)
{
if (amount > 0)
Balance += amount;
}
}
class Program
{
static void Main()
{
BankAccount account =
new BankAccount("Ravi");
account.Deposit(5000);
Console.WriteLine(account.AccountHolder);
Console.WriteLine(account.Balance);
}
}
5Check the Account Details
The constructor sets the account holder and initial balance. Deposit updates the balance through the public method.
Ravi 5000
6Watch Object Creation Step by Step
Follow how the constructor initialises the account and how the Deposit method updates its balance.
⌕
⑂
⚙
7Key Points to Remember
Constructors initialise objects when they are created. Properties provide controlled access to data. Access modifiers define who can use classes and their members.
- Constructors run during object creation.
- Use get and set to control property access.
- Choose access modifiers based on visibility requirements.
8Questions Interviewers May Ask
What is a constructor in C#?
A special class member with the same name as the class. It executes automatically when an object is created.
Can a constructor have a return type?
No. A constructor cannot have a return type, including void. It is used to initialise a new object.
What is the difference between a field and a property?
A field stores data directly. A property provides controlled access through get and set accessors.
What is the difference between private and protected?
Private members are accessible only inside their containing type. Protected members are also accessible in derived types.
What is the difference between internal and public?
Internal members are accessible within the same assembly. Public members are accessible wherever the containing type is accessible.