Edit

Share via


Compiler Error CS0051

Inconsistent accessibility: parameter type 'type' is less accessible than method 'method'

This error occurs when you declare a method (including constructors) with a parameter type that's less accessible than the method itself. For example, you might have a public constructor that uses an internal or private class as a parameter type.

The most common scenario is when you define a public method but one of its parameter types is internal or private. This creates an inconsistency because external code can see the method but can't access the types needed to call it.

How to troubleshoot this error

  1. Identify the parameter type causing the issue: Look at the error message to see which parameter type is less accessible.
  2. Check the accessibility of the parameter type: Right-click on the parameter type in your IDE and select "Go to Definition" (or press F12) to see how it's declared.
  3. Compare accessibility levels: Ensure the parameter type is at least as accessible as the method that uses it.

Examples

Example 1: Public method with private parameter type

The following sample generates CS0051 because the method F is public but the parameter type B is private:

// CS0051.cs
public class A
{
    // B is implicitly private here.
    class B
    {
    }

    public static void F(B b)  // CS0051
    {
    }

    public static void Main()
    {
    }
}

Example 2: Public constructor with internal parameter type

This is a common scenario where you have a public constructor but the parameter type is internal:

// Another file or assembly.
internal class DatabaseConfiguration
{
    public string ConnectionString { get; set; }
}

// In your main class.
public class DataService
{
    // This causes CS0051 because the constructor is public.
    // but DatabaseConfiguration is internal.
    public DataService(DatabaseConfiguration config)  // CS0051
    {
        // Implementation.
    }
}

To correct this error

Choose one of the following approaches:

  1. Make the parameter type more accessible: Change the parameter type to match or exceed the accessibility of the method:

    public class A
    {
        // Make B public to match the accessibility of method F.
        public class B
        {
        }
    
        public static void F(B b)  // Now works correctly
        {
        }
    }
    
  2. Reduce the accessibility of the method: Make the method less accessible to match the parameter type:

    public class A
    {
        class B  // B remains private.
        {
        }
    
        // Make F internal or private to match B's accessibility.
        internal static void F(B b)  // Now works correctly
        {
        }
    }
    
  3. Use a more accessible interface or base class: Instead of using the less accessible type directly, use a public interface or base class:

    public interface IConfiguration
    {
        string ConnectionString { get; }
    }
    
    internal class DatabaseConfiguration : IConfiguration
    {
        public string ConnectionString { get; set; }
    }
    
    public class DataService
    {
        // Use the public interface instead.
        public DataService(IConfiguration config)  // Works correctly
        {
            // Implementation.
        }
    }
    

See also