Access modifiers (public, private, internal) in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use access modifiers like public, private, and internal, we control who can use parts of our code.
We want to understand how these choices affect the time it takes for the program to run.
Analyze the time complexity of the following code snippet.
class Sample {
private int secretNumber = 42;
public int GetNumber() {
return secretNumber;
}
internal void PrintNumber() {
System.Console.WriteLine(secretNumber);
}
}
This code defines a class with different access levels for its members.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated operations here.
- How many times: Each method runs once when called.
Since there are no loops or repeated steps, the time to run these methods stays the same no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Constant steps |
| 100 | Constant steps |
| 1000 | Constant steps |
Pattern observation: The time does not grow with input size; it stays steady.
Time Complexity: O(1)
This means the time to run these methods stays the same no matter how big the input is.
[X] Wrong: "Making something private or internal makes the code slower because it adds checks."
[OK] Correct: Access modifiers only control who can use the code; they do not add extra steps that slow down the program.
Understanding that access modifiers do not affect how fast code runs helps you focus on what really matters when writing efficient programs.
"What if the methods contained loops that depended on input size? How would the time complexity change?"
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
