0
0
C Sharp (C#)programming~30 mins

Access modifiers (public, private, internal) in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Access Modifiers in C#
📖 Scenario: You are creating a simple class to represent a bank account. You want to control how the account's balance can be accessed and changed by using access modifiers.
🎯 Goal: Build a C# class BankAccount that uses public, private, and internal access modifiers to protect and expose data correctly.
📋 What You'll Learn
Create a class called BankAccount with a private field for balance
Add a public method to deposit money
Add an internal method to display the balance
Add a public method to withdraw money with balance check
💡 Why This Matters
🌍 Real World
Access modifiers help protect sensitive data and control how parts of a program interact, like keeping bank account balances safe.
💼 Career
Understanding access modifiers is essential for writing secure and maintainable code in professional software development.
Progress0 / 4 steps
1
Create the BankAccount class with a private balance field
Create a class called BankAccount with a private decimal field named balance initialized to 0.
C Sharp (C#)
Need a hint?

Use private decimal balance = 0m; inside the class.

2
Add a public method to deposit money
Add a public method called Deposit that takes a decimal parameter amount and adds it to the private balance.
C Sharp (C#)
Need a hint?

Use public void Deposit(decimal amount) and add amount to balance.

3
Add an internal method to display the balance
Add an internal method called DisplayBalance that returns the current balance as a decimal.
C Sharp (C#)
Need a hint?

Use internal decimal DisplayBalance() to return balance.

4
Add a public method to withdraw money with balance check and print the balance
Add a public method called Withdraw that takes a decimal amount. It should subtract amount from balance only if balance is enough. Then, create a BankAccount object, deposit 100, withdraw 30, and print the balance using DisplayBalance().
C Sharp (C#)
Need a hint?

Use public void Withdraw(decimal amount) with an if to check balance. Then create BankAccount object, deposit 100, withdraw 30, and print balance.