LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Constructors, Properties and Access Modifiers

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.

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
Remember If no access modifier is specified, class members are private by default. Top-level classes are internal by default.

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.

Program.csC#
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);
    }
}
Pro Tip Use private setters for values that should change only through business methods, such as depositing money into an account.

5Check the Account Details

The constructor sets the account holder and initial balance. Deposit updates the balance through the public method.

Output
Ravi
5000
Common Mistake A constructor does not have a return type, not even void. Adding a return type makes it a normal method.

6Watch Object Creation Step by Step

Follow how the constructor initialises the account and how the Deposit method updates its balance.

● ● ●Program.cs - Visual Studio Code
▸ Program.cs
▣
⌕
⑂
⚙
 
PROBLEMS  OUTPUT  DEBUG CONSOLE  TERMINAL
 
 
 
Ln 27, Col 9  UTF-8  C#

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

Q1Easy

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.

Q2Easy

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.

Q3Medium

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.

Q4Medium

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.

Q5Hard

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.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.