Skip to content

Commit 782b0bd

Browse files
CopilotBillWagnergewarren
authored
Enhance CS0051 documentation with clearer explanations and troubleshooting guidance (#47629)
* Initial plan * Enhance CS0051 documentation with better explanations and troubleshooting steps Co-authored-by: BillWagner <[email protected]> * Fix wording to use 'method' instead of 'constructor' for generality Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * Add periods to code comments as requested Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent cc45c64 commit 782b0bd

File tree

1 file changed

+102
-5
lines changed
  • docs/csharp/language-reference/compiler-messages

1 file changed

+102
-5
lines changed

docs/csharp/language-reference/compiler-messages/cs0051.md

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Compiler Error CS0051"
33
title: "Compiler Error CS0051"
4-
ms.date: 07/20/2015
4+
ms.date: 08/05/2025
55
f1_keywords:
66
- "CS0051"
77
helpviewer_keywords:
@@ -12,17 +12,26 @@ ms.assetid: 62182e8d-c4a5-4853-a990-fd57a4f7c3b8
1212

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

15-
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).
15+
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.
1616

17-
## Example
17+
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.
1818

19-
The following sample generates CS0051:
19+
## How to troubleshoot this error
20+
21+
1. **Identify the parameter type causing the issue**: Look at the error message to see which parameter type is less accessible.
22+
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.
23+
1. **Compare accessibility levels**: Ensure the parameter type is at least as accessible as the method that uses it.
24+
25+
## Examples
26+
27+
### Example 1: Public method with private parameter type
28+
29+
The following sample generates CS0051 because the method `F` is public but the parameter type `B` is private:
2030

2131
```csharp
2232
// CS0051.cs
2333
public class A
2434
{
25-
// Try making B public since F is public
2635
// B is implicitly private here.
2736
class B
2837
{
@@ -37,3 +46,91 @@ public class A
3746
}
3847
}
3948
```
49+
50+
### Example 2: Public constructor with internal parameter type
51+
52+
This is a common scenario where you have a public constructor but the parameter type is internal:
53+
54+
```csharp
55+
// Another file or assembly.
56+
internal class DatabaseConfiguration
57+
{
58+
public string ConnectionString { get; set; }
59+
}
60+
61+
// In your main class.
62+
public class DataService
63+
{
64+
// This causes CS0051 because the constructor is public.
65+
// but DatabaseConfiguration is internal.
66+
public DataService(DatabaseConfiguration config) // CS0051
67+
{
68+
// Implementation.
69+
}
70+
}
71+
```
72+
73+
## To correct this error
74+
75+
Choose one of the following approaches:
76+
77+
1. **Make the parameter type more accessible**: Change the parameter type to match or exceed the accessibility of the method:
78+
79+
```csharp
80+
public class A
81+
{
82+
// Make B public to match the accessibility of method F.
83+
public class B
84+
{
85+
}
86+
87+
public static void F(B b) // Now works correctly
88+
{
89+
}
90+
}
91+
```
92+
93+
1. **Reduce the accessibility of the method**: Make the method less accessible to match the parameter type:
94+
95+
```csharp
96+
public class A
97+
{
98+
class B // B remains private.
99+
{
100+
}
101+
102+
// Make F internal or private to match B's accessibility.
103+
internal static void F(B b) // Now works correctly
104+
{
105+
}
106+
}
107+
```
108+
109+
1. **Use a more accessible interface or base class**: Instead of using the less accessible type directly, use a public interface or base class:
110+
111+
```csharp
112+
public interface IConfiguration
113+
{
114+
string ConnectionString { get; }
115+
}
116+
117+
internal class DatabaseConfiguration : IConfiguration
118+
{
119+
public string ConnectionString { get; set; }
120+
}
121+
122+
public class DataService
123+
{
124+
// Use the public interface instead.
125+
public DataService(IConfiguration config) // Works correctly
126+
{
127+
// Implementation.
128+
}
129+
}
130+
```
131+
132+
## See also
133+
134+
- [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md)
135+
- [Accessibility Levels](../keywords/accessibility-levels.md)
136+
- [Constructors](../../programming-guide/classes-and-structs/constructors.md)

0 commit comments

Comments
 (0)