Skip to content

Commit 2f9eb93

Browse files
CopilotBillWagnergewarren
authored
Add generic type argument accessibility example to CS0050 documentation (#47705)
* Initial plan * Add generic type argument accessibility example to CS0050 documentation Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[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 b1f9c7c commit 2f9eb93

File tree

1 file changed

+42
-2
lines changed
  • docs/csharp/language-reference/compiler-messages

1 file changed

+42
-2
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ ms.assetid: dead2d28-f4db-4afe-b8dd-38968625f7c3
1212

1313
Inconsistent accessibility: return 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. For more information, see [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md).
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. This includes type arguments of generic types used in the return type or parameters. For more information, see [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md).
1616

17-
## Example
17+
## Examples
1818

1919
The following sample generates CS0050 because no accessibility modifier is supplied for `MyClass`, and its accessibility therefore defaults to `private`:
2020

@@ -36,3 +36,43 @@ public class MyClass2
3636
public static void Main() { }
3737
}
3838
```
39+
40+
CS0050 can also occur when a generic type's type argument is less accessible than the method:
41+
42+
```csharp
43+
// CS0050_Generic.cs
44+
using System.Collections.ObjectModel;
45+
46+
internal class CeisData // Internal class
47+
{
48+
public string Name { get; set; }
49+
}
50+
51+
public class MyClass
52+
{
53+
public static ObservableCollection<CeisData> BuildCeis() // CS0050
54+
{
55+
return new ObservableCollection<CeisData>();
56+
}
57+
}
58+
```
59+
60+
To fix this error, make the type argument at least as accessible as the method:
61+
62+
```csharp
63+
// Fixed version
64+
using System.Collections.ObjectModel;
65+
66+
public class CeisData // Now public
67+
{
68+
public string Name { get; set; }
69+
}
70+
71+
public class MyClass
72+
{
73+
public static ObservableCollection<CeisData> BuildCeis() // OK
74+
{
75+
return new ObservableCollection<CeisData>();
76+
}
77+
}
78+
```

0 commit comments

Comments
 (0)