public/internal/protected/private (with note on file)
Learn how public, internal, protected, and private change visibility; see inheritance and same-assembly rules. Note that file-scoped access is not in C# 6.
public/internal/protected/private (with note on file) is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Access modifiers overview
Goal: Pick the right visibility.
- public: anywhere
- internal: same assembly
- protected: derived classes
- private: same class
- file: newer than C# 6 (info only)
private vs public
Keep fields private and expose validated public members.
using System;
// private: only inside the class; public: everywhere the class is visible
public class Box
{
private int _width; // hidden storage
public int Width // controlled access
{
get { return _width; }
set { _width = value < 0 ? 0 : value; } // simple guard
}
}
public class Program
{
public static void Main(string[] args)
{
Box b = new Box();
// b._width = -5; // INVALID: private (kept as comment)
b.Width = 10; // OK: public property
Console.WriteLine("Width = " + b.Width);
}
}
protected in inheritance
protected is for inheritance: derived types can use it, outsiders cannot.
using System;
public class BaseCounter
{
protected int Count; // derived classes can see this
public int Value() { return Count; }
}
public class StepCounter : BaseCounter
{
public void Step() { Count = Count + 1; } // allowed: protected
}
public class Program
{
public static void Main(string[] args)
{
StepCounter sc = new StepCounter();
sc.Step();
// sc.Count = 5; // INVALID from outside (commented)
Console.WriteLine("Value = " + sc.Value());
}
}
internal access
internal limits visibility to the current assembly (library or app build).
using System;
// internal type: visible only inside this assembly
internal class InternalHelper
{
public static string Ping() { return "pong"; }
}
// public type can use internal from the same assembly
public class UsesInternal
{
public string Call() { return InternalHelper.Ping(); }
}
public class Program
{
public static void Main(string[] args)
{
UsesInternal u = new UsesInternal();
Console.WriteLine(u.Call());
}
}
protected internal
protected internal means: accessible by derived types or anywhere in the same assembly.
using System;
public class LibraryBase
{
// Accessible to derived types OR anywhere in the same assembly
protected internal void Touch() { Console.WriteLine("touched"); }
}
public class LibChild : LibraryBase
{
public void Use() { Touch(); } // via inheritance
}
public class Program
{
public static void Main(string[] args)
{
LibChild c = new LibChild();
c.Use();
LibraryBase b = new LibraryBase();
b.Touch(); // same assembly, allowed
}
}
file modifier note & tips
Note: The file access modifier arrived in newer C# (not in C# 6). For C# 6 stick to public, internal, protected, private, and protected internal.
- Prefer private by default
- Use public only for API surface
- Use internal to hide implementation from other assemblies
- Use protected for base-to-derived cooperation
internal meaning
Recap
Recap: Choose the smallest needed visibility: private → protected/internal → public. C# 6 does not include the newer file modifier; rely on internal for assembly-level hiding.
Frequently asked questions
Is the “public/internal/protected/private (with note on file)” lesson free?
Yes — the full text of “public/internal/protected/private (with note on file)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “public/internal/protected/private (with note on file)”?
Learn how public, internal, protected, and private change visibility; see inheritance and same-assembly rules. Note that file-scoped access is not in C# 6. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “public/internal/protected/private (with note on file)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- public/internal/protected/private (with note on file)
- Namespaces, using directives, global usings (note)
- Partial types & files