skip to content

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%

answer

  1. a signature is part of the member
  2. callers must be able to name every type
  3. return, parameter, field, property, base class
  4. narrow the member or widen the type

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.

solid answer

~40 s

The rule is called **inconsistent accessibility**: a member's return type, parameter types, field or property type, and a class's base class must each be at least as accessible as the declaration that uses them. `public RateCard Load()` with an `internal class RateCard` is CS0050; the same type as a parameter is CS0051, as a field type CS0052, as a property type CS0053, and as a base class CS0060. `protected` members count as outward-facing too, because derived classes in other assemblies can call them. The fixes: make the member `internal`, make the type `public`, or return a public interface or base class that the internal type implements.

code

csharp · 11 lines
csharp
Console.WriteLine("never runs");

internal class RateCard { }

public class Pricing
{
    public RateCard Load() => new();            // CS0050: return type
    public void Save(RateCard card) { }         // CS0051: parameter type
    protected void Apply(RateCard card) { }     // CS0051: protected is visible outside too
    internal RateCard LoadForTax() => new();    // fine: member is no wider than the type
}

go deeper

for a junior

Recall the rule in one line: a member's signature cannot mention a type that is less visible than the member, and the fix is to narrow the member or widen the type.

for a middle

Name the diagnostic family, CS0050 to CS0053 and CS0060, and explain why protected members count as outward-facing while internal and private protected ones do not.

for a senior

In review, treat CS0050 as a design signal: prefer narrowing the member or returning a public abstraction over publishing the implementation type.

for a principal

Decide which types form the library's contract; every type a public signature names is a long-term compatibility commitment, so the abstraction fix is often a policy, not a one-off.

## The rule behind CS0050 Every member has an **accessibility domain**: the program text from which it can be referenced. When a member's signature names other types, C# demands that each of those types be visible everywhere the member is visible. Otherwise a caller could receive an object, or be asked to pass one, whose type it is not allowed to name. The C# reference lists these as the **restrictions on using accessibility levels**, and the compiler enforces them at the declaration, not at the call site. For a `public` method in a `public` class, "everywhere" means every assembly that references this one. An `internal` type is visible only inside its own assembly, so the pairing fails. ## The family of diagnostics The SDK reports a different number for each place a type can appear: | Where the less accessible type appears | Diagnostic | |---|---| | Method return type | CS0050 | | Method or indexer parameter | CS0051 | | Field type | CS0052 | | Property type | CS0053 | | Base class of a class | CS0060 | The message text names both sides, for example "Inconsistent accessibility: return type 'RateCard' is less accessible than method 'Pricing.Load()'". Generic type arguments count as well: `public List<RateCard> All()` is CS0050 because `List<RateCard>` is only as accessible as its least accessible argument. ## Which members are outward-facing The check uses the member's **effective** accessibility, not just the keyword: - `public` members of a `public` type are visible to other assemblies, so their signatures must use public types. - `protected` and `protected internal` members of a `public` type are also visible to other assemblies, through derived classes. `protected void Apply(RateCard card)` with an internal `RateCard` is CS0051. - `internal` and `private protected` members stay inside the assembly, so they may use internal types freely. - A `public` member of an `internal` class is effectively internal, so it may return internal types without error. A public class nested inside an internal class is effectively internal too. Returning `Hidden.Shown` from a public method of a public class is still CS0050, even though `Shown` itself is declared `public`. ## Three ways to fix it 1. **Narrow the member.** If no other assembly should call `Load`, declare it `internal`. This is the right fix more often than people expect: the error frequently reveals a helper that was made `public` by habit. 2. **Widen the type.** If callers in other assemblies genuinely need `RateCard`, make it `public`, and accept that its shape is now part of your compatibility promise. 3. **Expose an abstraction.** Declare a `public interface IRateCard` (or a public abstract base), keep `RateCard` as an `internal sealed class` implementing it, and return `IRateCard`. Callers get what they need; the implementation stays private to the assembly. What does **not** fix it: - adding `[InternalsVisibleTo]` for a friend assembly, because the member is still visible to every other assembly as well; - marking the type `protected internal`, which is not allowed on a top-level type (CS1527); - suppressing the diagnostic, because CS0050 is an error, not a warning. ## Beyond methods and properties The C# reference lists the same restriction for every kind of declaration that names a type: - the explicit base interfaces of an interface must be at least as accessible as the interface; - a delegate's return and parameter types must be at least as accessible as the delegate type; - the types of constants, fields, properties and events must be at least as accessible as the member; - an indexer's type and parameter types, an operator's return and parameter types, and a constructor's parameter types must be at least as accessible as the indexer, operator or constructor. So a `public` constructor taking an internal options class, or a `public event` whose delegate type is internal, fails for the same reason a method does. ## The same check on file-local types From C# 11, a type declared with the `file` modifier is visible only in its source file. Using one in the signature of a non-file-local type draws the dedicated CS9051 alongside the familiar CS0050, and using one as a base class draws CS9053 alongside CS0060. The underlying rule is the same: a signature may not leak a type to code that cannot name it. ## Reading the error in review When a pull request adds a `public` member and the build fails with CS0050 or CS0051, ask which of the three fixes matches the intent. Widening the type is the easiest keystroke and usually the wrong choice for a library: it publishes an implementation detail that the abstraction or the narrower member would have kept private.

  • Why does a protected method with an internal parameter type fail, when protected looks narrower than public?
    Because a `protected` member of a `public` class is reachable from derived classes in any assembly. Those classes would have to pass an argument of a type they cannot name, so the parameter type must be at least as accessible as the member's effective domain. The compiler reports CS0051; declaring the method `private protected` or `internal` removes the outside reach and the error.
  • A public class nested in an internal class is returned by a public method; does that compile?
    No. A nested type's accessibility domain never exceeds its containing type's, so the nested `public` class is effectively internal. A public method of a public class returning it is CS0050, with the nested type's full name in the message. Make the containing type public or move the nested type out.

  • Says CS0050 is a warning that can be suppressed.
  • Thinks InternalsVisibleTo lets a public method return an internal type.
  • Believes protected members may freely use internal types in their signatures.
  • Fixes every CS0050 by making the internal type public.
  • Thinks a public nested class inside an internal class is visible to other assemblies.