Skip to content

Enhance CS0051 documentation with clearer explanations and troubleshooting guidance #47629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 102 additions & 5 deletions docs/csharp/language-reference/compiler-messages/cs0051.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: "Compiler Error CS0051"
title: "Compiler Error CS0051"
ms.date: 07/20/2015
ms.date: 08/05/2025
f1_keywords:
- "CS0051"
helpviewer_keywords:
Expand All @@ -12,17 +12,26 @@ ms.assetid: 62182e8d-c4a5-4853-a990-fd57a4f7c3b8

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

The return type and each of the types referenced in the formal parameter list of a method must be at least as accessible as the method itself. Make sure the types used in method signatures are not accidentally private due to the omission of the `public` modifier. For more information, see [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md).
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.

## Example
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.

The following sample generates CS0051:
## 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.
1. **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.
1. **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:

```csharp
// CS0051.cs
public class A
{
// Try making B public since F is public
// B is implicitly private here.
class B
{
Expand All @@ -37,3 +46,91 @@ public class A
}
}
```

### 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:

```csharp
// 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:

```csharp
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
{
}
}
```

1. **Reduce the accessibility of the method**: Make the method less accessible to match the parameter type:

```csharp
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
{
}
}
```

1. **Use a more accessible interface or base class**: Instead of using the less accessible type directly, use a public interface or base class:

```csharp
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

- [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md)
- [Accessibility Levels](../keywords/accessibility-levels.md)
- [Constructors](../../programming-guide/classes-and-structs/constructors.md)