skip to content

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%

answer

  1. public is a promise
  2. library vs application assemblies
  3. tests coupled to implementation
  4. friends quieten some analyzers

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.

solid answer

~40 s

I would make `internal` the default and require a reason for `public`, because every public type and signature in a library is a compatibility promise, and inconsistent-accessibility errors (CS0050 and friends) then keep internals from leaking by accident. In an application assembly nobody references, `public` buys nothing, and CA1515 can flag it. For tests I would allow `InternalsVisibleTo` to test assemblies only, never to production assemblies, and prefer testing through the public surface where it is natural. The costs to name: tests break on internal refactors, strong-named libraries need key management for the friend, and CA1812 and CA1852 stop reporting internal types once any friend exists unless `dotnet_code_quality.ignore_internalsvisibleto = true` is set.

go deeper

for a junior

Know that a type with no modifier is internal and that tests can reach internal code through InternalsVisibleTo in the library.

for a middle

Explain why public types are harder to change than internal ones, and how CS0050-style errors stop internals leaking through public signatures.

for a senior

Configure friend grants for test projects, handle strong-named keys, and know which analyzers change behavior when a friend exists.

for a principal

Own the standard: internal by default, friends for tests only, analyzers set to ignore friends, and every widening reviewed as an API change, adjusted for libraries versus applications.

## The decision A coding standard has to answer two linked questions: 1. **What accessibility does a new type get when nobody thinks about it?** C# already defaults top-level types to `internal`, but tooling and habit often produce `public class` everywhere. 2. **How do tests reach code that is not public?** Through the public surface only, or through `InternalsVisibleTo` friend grants? There is no single right answer; the shape of the solution decides it. ## The case for internal by default - **A public type is a promise.** Once another assembly compiles against a `public` type or member, renaming it, changing its signature or narrowing it breaks that consumer. `internal` types can change freely within one build. - **The compiler polices leaks.** With most types internal, a `public` member that mentions one is rejected (CS0050, CS0051, CS0053, CS0060). That turns an accidental exposure into a build error the author has to think about. - **Applications gain nothing from public.** An executable assembly is rarely referenced by another, so `public` is noise there. The analyzer rule **CA1515** ("Consider making public types internal") targets exactly this case; it is not enabled by default in .NET 10, so a team that wants it turns it on. - **Internal types unlock other analysis.** **CA1852** suggests sealing internal types with no subtypes, and **CA1812** flags internal classes that are never instantiated. Both can only reason about types no other assembly can see. ## The case against, or for caution - **Friction for genuine sharing.** Solutions with several libraries need some types public anyway; a blanket rule creates churn when the second consumer appears. - **Tests need a path in.** Logic hidden behind `internal` must be tested through the public surface or through a friend grant. ## InternalsVisibleTo for tests: the trade-offs | Option | Gains | Costs | |---|---|---| | Test only through the public surface | Tests survive internal refactors; they describe behavior callers rely on | Some logic is awkward to reach; setup grows | | `InternalsVisibleTo` to test assemblies | Direct tests of internal units; nothing widened for real consumers | Tests coupled to implementation; key management for strong-named libraries | | Make the type `public` "for testing" | No configuration | Publishes an implementation detail permanently | Facts that shape the choice: - A friend grant is per assembly, written in the library: one `InternalsVisibleTo` per test project, ideally generated from an `<InternalsVisibleTo Include="..." />` item so it sits with the build settings. - A strong-named library must put the friend's full public key in the grant (CS1726 otherwise) and the test assembly must be signed to match (CS0281 otherwise). - **Analyzers go quiet.** CA1812 and CA1852 skip internal types by default once the assembly has any `InternalsVisibleTo`, because a friend might use them. Setting `dotnet_code_quality.ignore_internalsvisibleto = true` in `.editorconfig` restores those warnings; without it, the friend grant hides dead and unsealed internal types. ## A standard I would defend 1. New types are `internal` (or `file` for generated, single-file helpers); `public` requires that another assembly needs the type. 2. In application assemblies, enable CA1515 so stray `public` declarations surface. 3. Friend grants go to **test assemblies only**, declared in the csproj, never to production assemblies. 4. Prefer tests through public behavior; use friend access for internal units with real logic, not to inspect state. 5. Turn on CA1852 and CA1812 with `ignore_internalsvisibleto` set to `true`, so friend grants do not hide dead or unsealed types. 6. Review every widening from `internal` to `public` as an API change. ## Signals that the standard is working - Pull requests that widen a type from `internal` to `public` are rare and each one carries a reason. - Inconsistent-accessibility errors show up in review as design conversations, not as a reflex to make another type `public`. - Test projects are the only friends, and analyzer warnings about unsealed or unused internal types are fixed rather than hidden. The open questions a lead should still weigh: how much test coupling the team tolerates, whether libraries are published outside the solution (which raises the cost of every public type), and whether strong naming is required, which makes friend grants heavier to maintain.

  • A team grants InternalsVisibleTo to its tests and notices CA1852 stopped firing; why, and what do you change?
    CA1852 and CA1812 assume a friend assembly might derive from or instantiate internal types, so by default they skip them once any `InternalsVisibleTo` exists. Setting `dotnet_code_quality.ignore_internalsvisibleto = true` in `.editorconfig` makes them analyze the assembly anyway. With test-only friends that is the right setting, because tests should not be the reason a type stays unsealed or unused.
  • When would you allow a production assembly to be a friend?
    Rarely: when two assemblies always ship together as one unit and splitting them is a packaging detail. Even then the friend becomes an undeclared consumer of internals, so I would rather extract the shared types into a small public contract or merge the assemblies than grow a web of friend grants.

  • Makes types public so that tests can reach them.
  • Grants InternalsVisibleTo to production assemblies to avoid designing an API.
  • Assumes analyzers keep reporting unused internal types after a friend grant.
  • Treats internal-by-default as free, ignoring test coupling and key management.
  • Believes public types in an executable carry the same compatibility cost as in a library.