BankAccount with a private field for balanceAccess modifiers (public, private, internal) in C Sharp (C#) - Mini Project: Build & Apply
Start learning this pattern below
Jump into concepts and practice - no test required
BankAccount with a private decimal field named balance initialized to 0.Use private decimal balance = 0m; inside the class.
Deposit that takes a decimal parameter amount and adds it to the private balance.Use public void Deposit(decimal amount) and add amount to balance.
DisplayBalance that returns the current balance as a decimal.Use internal decimal DisplayBalance() to return balance.
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().Use public void Withdraw(decimal amount) with an if to check balance. Then create BankAccount object, deposit 100, withdraw 30, and print balance.
Practice
Which access modifier allows a class member to be accessed from any other code in the same project or assembly?
Solution
Step 1: Understand the meaning of internal
Theinternalmodifier allows access within the same assembly or project but not outside it.Step 2: Compare with other modifiers
privaterestricts access to the same class only,publicallows access from anywhere, andprotectedallows access in derived classes.Final Answer:
internal -> Option AQuick Check:
Access inside project = internal [OK]
- Confusing internal with public
- Thinking private allows access outside class
- Mixing protected with internal
Which of the following is the correct way to declare a private integer field named count in a class?
?
Solution
Step 1: Recall correct syntax for access modifiers
The access modifier comes first, then the type, then the variable name.Step 2: Check each option's order and keywords
private int count; follows correct order:private int count;. Others have incorrect order or missing type.Final Answer:
private int count; -> Option DQuick Check:
Access modifier + type + name [OK]
- Placing access modifier after type
- Omitting the type
- Using invalid order of keywords
Consider the following code snippet:
class MyClass {
private int secret = 42;
public int GetSecret() {
return secret;
}
}
MyClass obj = new MyClass();
Console.WriteLine(obj.secret);What will happen when this code runs?
Solution
Step 1: Identify access modifier of 'secret'
The fieldsecretis declaredprivate, so it cannot be accessed outsideMyClass.Step 2: Check code accessing 'secret'
The code tries to accessobj.secretoutside the class, which is not allowed and causes a compile-time error.Final Answer:
Compilation error: 'secret' is inaccessible due to its protection level -> Option CQuick Check:
Private fields cannot be accessed outside class [OK]
- Assuming private fields are accessible outside class
- Confusing runtime errors with compile errors
- Thinking public methods expose private fields directly
Given this code snippet, identify the error and fix it:
class Sample {
internal int value;
}
class Test {
void Show() {
Sample s = new Sample();
Console.WriteLine(s.value);
}
}Assuming these classes are in different projects, what is the problem?
Solution
Step 1: Understand 'internal' access modifier scope
internalallows access only within the same project or assembly.Step 2: Check class locations
SinceSampleandTestare in different projects,Testcannot accessinternalmembers ofSample.Step 3: Fix the access level
Changingvaluetopublicallows access from other projects.Final Answer:
Error: 'value' is inaccessible due to protection level; fix by changing 'internal' to 'public' -> Option BQuick Check:
Internal = same project only; public = accessible everywhere [OK]
- Assuming internal allows cross-project access
- Changing internal to private incorrectly
- Confusing static with access modifiers
You have a class library project with a class Helper that has a method Calculate() marked as internal. You want to allow another project in the same solution to use Calculate() without making it public. What is the best way to achieve this?
Solution
Step 1: Understand internal and project boundaries
internalrestricts access to the same assembly, so other projects cannot access it by default.Step 2: Use InternalsVisibleTo attribute
This attribute allows you to specify friend assemblies that can access internal members without making them public.Step 3: Evaluate other options
Changing to public exposes to all, private hides too much, moving method is impractical.Final Answer:
Use the InternalsVisibleTo attribute to expose internal members to the other project -> Option AQuick Check:
InternalsVisibleTo grants internal access to specific projects [OK]
- Making method public unnecessarily
- Thinking private allows cross-project access
- Moving code instead of using attributes
