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

Access modifiers (public, private, internal) in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Access modifiers (public, private, internal)
Start
Define class members
Apply access modifiers
public: accessible anywhere
private: accessible only inside class
internal: accessible inside same assembly
Use members
Access allowed?
Yes/No
End
This flow shows how class members are defined with access modifiers and how access is checked based on modifier rules.
Execution Sample
C Sharp (C#)
class MyClass {
  public int a = 1;
  private int b = 2;
  internal int c = 3;
}

MyClass obj = new MyClass();
Console.WriteLine(obj.a);
Defines a class with public, private, and internal members and accesses the public member from outside.
Execution Table
StepActionMember AccessedAccess ModifierAccess AllowedOutput/Result
1Create object objN/AN/AN/Aobj created
2Access obj.aapublicYesPrints 1
3Access obj.bbprivateNoCompile error: 'b' is inaccessible
4Access obj.ccinternalYes if same assembly, else NoPrints 3 or error
5EndN/AN/AN/AExecution stops
💡 Execution stops after access attempts; private member access fails outside class.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
objnullMyClass instanceMyClass instanceMyClass instanceMyClass instance
obj.aN/A1111
obj.bN/A2 (private)2 (private)2 (private)2 (private)
obj.cN/A3 (internal)3 (internal)3 (internal)3 (internal)
Key Moments - 3 Insights
Why can't we access the private member 'b' from outside the class?
Because 'b' is marked private, it is only accessible inside the class. The execution_table row 3 shows the compile error when trying to access it outside.
When can we access the internal member 'c'?
The internal member 'c' can be accessed anywhere inside the same assembly (project). If accessed from outside the assembly, access is denied as shown in execution_table row 4.
What does the public modifier allow?
Public members like 'a' can be accessed from anywhere, inside or outside the class and assembly, as shown in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens when we try to access obj.b?
ACompile error because 'b' is private
BAccess is allowed and value 2 is printed
CAccess is allowed only inside the same assembly
DValue 3 is printed
💡 Hint
Check execution_table row 3 where accessing obj.b causes a compile error.
At which step does the program print the value of the public member?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row 2 where obj.a is accessed and prints 1.
If the internal member 'c' is accessed from a different assembly, what will happen?
AAccess allowed and prints 3
BCompile error due to internal access restriction
CCompile error due to private access
DAccess allowed only if public
💡 Hint
Refer to execution_table row 4 explaining internal access depends on assembly.
Concept Snapshot
Access modifiers control who can use class members:
- public: accessible anywhere
- private: accessible only inside the class
- internal: accessible inside the same assembly
Use modifiers to protect or expose data safely.
Full Transcript
This visual execution shows how access modifiers in C# control access to class members. Public members can be accessed anywhere, private members only inside the class, and internal members only inside the same assembly. The example creates an object and tries to access members with different modifiers. Access to private members from outside causes a compile error. Internal members are accessible only if accessed within the same assembly. This helps protect data and control visibility in programs.