skip to content

Visibility Levels

public, internal, protected, private, protected internal, private protected and file decide who sees a member. Interviewers ask about internal across assemblies.

on this pageshow

explore

questions

6

In C#, what access level does a type, a class member, an interface member and a nested type get when no access modifier is written?

level: juniorimportance: must knowfreq 72%

answer

  1. the default depends on where it is declared
  2. namespace level vs inside a type
  3. assembly for types, containing type for members
  4. interfaces flip the member default

basics

~10 s

A top-level class, struct, interface, enum or delegate defaults to internal. A member of a class or struct, including a nested type, defaults to private. Interface members and enum members default to public.

solid answer

~30 s

C# picks the default from the declaration's position. A type declared directly in a namespace may only be `public` or `internal`, and with no modifier it is `internal`: visible to the whole assembly, invisible outside it. Writing `private` or `protected` there is error CS1527. Inside a class, struct or record, a member declared with no modifier is `private`, and that includes nested types. Interface members are `public` unless you write otherwise, and enum members are always `public`. A member is never more visible than its containing type, so a `public` method on an `internal` class is still unreachable from another assembly.

code

csharp · 24 lines
csharp
using System.Reflection;

const BindingFlags All = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
Type calc = typeof(InvoiceCalculator);

Console.WriteLine($"InvoiceCalculator public:  {calc.IsPublic}");
Console.WriteLine($"rate private:              {calc.GetField("rate", All)!.IsPrivate}");
Console.WriteLine($"Cache nested private:      {calc.GetNestedType("Cache", All)!.IsNestedPrivate}");
Console.WriteLine($"IInvoiceStore.Save public: {typeof(IInvoiceStore).GetMethod("Save")!.IsPublic}");
Console.WriteLine($"Total: {new InvoiceCalculator().Total(100)}");

class InvoiceCalculator                 // top-level, no modifier: internal
{
    int rate = 20;                      // class member, no modifier: private

    class Cache                         // nested type, no modifier: private
    {
        public int Read(InvoiceCalculator c) => c.rate;   // nested code reads the outer private field
    }

    public int Total(int net) => net + net * new Cache().Read(this) / 100;
}

interface IInvoiceStore { void Save(); }  // interface member, no modifier: public

go deeper

for a junior

Recall the two positions: internal for a type in a namespace, private for a member of a class or struct. Then add the exceptions: interface and enum members are public.

for a middle

Explain the accessibility domain: a member is capped by its containing type, so public inside an internal class still stops at the assembly boundary. Mention CS1527 for private top-level types.

for a senior

Show you write modifiers explicitly on API surfaces and enforce it with IDE0040, and that you keep types internal until another assembly truly needs them.

for a principal

Frame the defaults as a team decision: an internal-by-default codebase keeps refactoring cheap, and the cost is deciding how tests and sibling assemblies reach those internals.

## Two questions decide the default C# has six **declared accessibility** levels (`public`, `internal`, `protected`, `protected internal`, `private protected`, `private`) and, from C# 11, the `file` modifier for top-level types. When a declaration carries no modifier, the compiler does not pick the most open or the most closed level everywhere. It asks **where the declaration sits**: 1. Is it a type declared directly in a namespace (a **top-level type**), or a member of another type? 2. If it is a member, what kind of type contains it: a class, struct, record, interface or enum? ## The defaults, position by position | Declared in | Default with no modifier | What you may write instead | |---|---|---| | A namespace (top-level type) | `internal` | `public` only (or `file`) | | A class or record class | `private` | all six levels | | A struct or record struct | `private` | `public`, `internal`, `private` | | An interface | `public` | all six; a `private` member needs a body | | An enum | `public` | nothing: enum members take no modifier | Some consequences worth saying out loud in an interview: - **`class InvoiceCalculator { }` in a namespace is `internal`.** Another project that references the assembly cannot name it. Only `public` widens it. - **A top-level type cannot be `private` or `protected`.** The compiler rejects it with CS1527: "Elements defined in a namespace cannot be explicitly declared as private, protected, protected internal, or private protected." - **A field written as `int rate;` inside a class is `private`.** The same is true for methods, properties, events, constructors and nested types. - **A nested type defaults to `private` inside a class or struct.** Only the containing type (and anything nested in it) can name it. - **Interface members default to `public`.** Since C# 8 you may write other access modifiers on interface members, but leaving them out still means `public`; a nested type declared inside an interface is `public` too. - **Structs cannot use the `protected` family.** A struct cannot be inherited, so a `protected` field in a struct is error CS0666. ## Nested types and the containing type A nested type is a member of its containing type, so it can use **everything the containing type can use**, including that type's `private` fields. The reverse is not true: the containing class has no special access to the nested class's `private` members, and touching one is CS0122 ("is inaccessible due to its protection level"). ## The member is capped by its type The **accessibility domain** of a member is the region of program text where it can be referenced. For a member of a nested or top-level type, the domain is the intersection of the member's declared level and the domain of its containing type. So: - a `public` method on an `internal` class can be called anywhere in the assembly and nowhere else; - a `public` class nested inside an `internal` class is effectively `internal`; - marking everything `public` inside an `internal` class is harmless to outsiders, but it is noise in review. ## Seeing it for yourself The snippet in the examples uses reflection to print what the compiler emitted for declarations with no modifiers. The class reports `IsPublic` as `False` because it is `internal`, the field reports `IsPrivate`, the nested class reports `IsNestedPrivate`, and the interface method reports `IsPublic`. The nested `Cache` class reads the outer class's private `rate` field, which is legal precisely because nested code shares the containing type's access. ## A three-project walkthrough Picture a solution with a `Billing.Core` class library, a `Billing.App` executable that references it, and a `Billing.Core.Tests` project. In `Billing.Core`, `class InvoiceCalculator` with no modifier is internal: - every file in `Billing.Core` can create it and call its `public` methods; - `Billing.App` cannot name it at all; referencing it there is CS0122; - `Billing.Core.Tests` cannot name it either, unless the library grants it friend access with `InternalsVisibleTo`; - `InvoiceCalculator`'s private `rate` field is reachable only from inside `InvoiceCalculator`, including its nested `Cache` class, and from nowhere else, friend or not. Changing the first line to `public class InvoiceCalculator` opens the type and its `public` members to `Billing.App`, while the field stays private. ## Habits that follow from the defaults - Write the modifier anyway on anything that is part of an API surface; the style rule IDE0040 can require explicit accessibility modifiers so nobody relies on memory. - Leave top-level types `internal` until another assembly genuinely needs them; widening later is easy, narrowing a published `public` type breaks callers. - Remember that `static` and `sealed` are not access modifiers: they change instantiation and inheritance, not who can see the type.

  • Does marking a C# class static or sealed change who can see it?
    No. `static` and `sealed` control instantiation and inheritance, not visibility; the access level still comes from the modifier or the default. A `static` class is implicitly sealed: `new Clock()` is CS0712, declaring a variable of its type is CS0723 and deriving from it is CS0709. Declaring a `protected` member in a `sealed` class only draws warning CS0628, because no derived class can ever use it.
  • Can the containing class read a private field of its nested class?
    No. Access flows inward only: nested code can use everything the containing type can, including its `private` members, but the containing type has no special rights over the nested type's members. Reading a nested type's `private` field from the outer class is CS0122. Make the member `internal` or `public` on the nested type, which is still hidden if the nested type itself is `private`.

  • Says a class with no modifier is public to any referencing project.
  • Says class members default to internal, like top-level types.
  • Thinks interface members default to private without a modifier.
  • Believes a top-level class can be declared private to hide it in a file.
  • Thinks a public method on an internal class is callable from other assemblies.
open as a page

Why does a public C# method that returns an internal type fail with CS0050, and what are the ways to fix it?

level: middleimportance: should knowfreq 40%

basics

~20 s

C# requires every type in a member's signature to be at least as accessible as the member itself. A public method returning an internal type would hand outsiders a type they cannot name, so the compiler rejects it with CS0050.

open as a page

In C#, how do protected internal and private protected differ, and which code in another assembly can reach each member?

level: middleimportance: should knowfreq 52%

basics

~20 s

protected internal means any code in the same assembly or a derived class anywhere. private protected (C# 7.2) means only derived classes in the same assembly, so another assembly's code never reaches it unless that assembly is a friend.

open as a page

A C# test project must call internal helpers in a strong-named class library; how do you grant access with InternalsVisibleTo, and what can still fail?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Add [assembly: InternalsVisibleTo("Billing.Core.Tests")] to the library, or an InternalsVisibleTo item in its project. A strong-named library must name the test assembly with its full public key (CS1726), and the test assembly must be signed with that key (CS0281).

open as a page

As the lead setting a C# solution's coding standard, would you make types internal by default and let tests reach them through InternalsVisibleTo?

level: principalimportance: should knowfreq 30%

basics

~20 s

Usually yes for types: internal by default keeps a library's public surface small and refactoring cheap. Granting InternalsVisibleTo to test assemblies only is a reasonable trade, but it couples tests to internals and silences some unused-type analyzers.

open as a page

In C# 11 and later, what does the file modifier restrict on a type, and why do source generators declare their helper types with it?

level: middleimportance: nice to knowfreq 20%

basics

~20 s

A type declared with the file modifier (C# 11) is visible only inside the source file that declares it. Two files can each declare a file-local Formatter without a name clash, which is why source generators use it for helper types.

open as a page