Skip to content

Commit 9fa88f9

Browse files
CopilotBillWagner
andauthored
Improve CS0119 error documentation with specific guidance for Type collections (#47308)
* Initial plan * Add specific example for CS0119 error with List<Type> collections Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]>
1 parent 7411ad9 commit 9fa88f9

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

docs/csharp/misc/cs0119.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ ms.assetid: 048924f1-378f-4021-bd20-299d3218f810
2020

2121
- A method identifier is used as if it were a struct or class
2222

23-
## Example
23+
## Examples
24+
25+
### Example 1
2426

2527
The following sample generates CS0119: 'C.B()' is a method, which is not valid in the given context. You can fix this error by changing the name of the method `C.B`, or using the fully qualified name for the class `B` like `N2.B`.
2628

@@ -42,3 +44,70 @@ namespace N1
4244
}
4345
}
4446
```
47+
48+
### Example 2
49+
50+
The following sample shows a common scenario where CS0119 occurs when trying to add class names to a collection of types. The compiler expects <xref:System.Type> instances, not class names.
51+
52+
```csharp
53+
using System;
54+
using System.Collections.Generic;
55+
56+
public class Page_BaseClass { }
57+
public class Page_CRTable { }
58+
public class Page_DBGridWithTools { }
59+
60+
public static class PageTypeManager
61+
{
62+
public static List<Type> GetPageTypes()
63+
{
64+
List<Type> pageTypesList = new List<Type>();
65+
66+
// CS0119: 'Page_BaseClass' is a type, which is not valid in the given context
67+
pageTypesList.Add(Page_BaseClass);
68+
pageTypesList.Add(Page_CRTable);
69+
pageTypesList.Add(Page_DBGridWithTools);
70+
71+
return pageTypesList;
72+
}
73+
}
74+
```
75+
76+
To fix this error, use the <xref:System.Type.GetType%2A?displayProperty=nameWithType> method or the `typeof` operator to get `Type` instances:
77+
78+
```csharp
79+
using System;
80+
using System.Collections.Generic;
81+
82+
public static class PageTypeManager
83+
{
84+
public static List<Type> GetPageTypes()
85+
{
86+
List<Type> pageTypesList = new List<Type>();
87+
88+
// Use typeof operator to get Type instances
89+
pageTypesList.Add(typeof(Page_BaseClass));
90+
pageTypesList.Add(typeof(Page_CRTable));
91+
pageTypesList.Add(typeof(Page_DBGridWithTools));
92+
93+
return pageTypesList;
94+
}
95+
96+
// Alternative: Load types dynamically by name
97+
public static List<Type> GetPageTypesByName(string[] typeNames)
98+
{
99+
List<Type> pageTypesList = new List<Type>();
100+
101+
foreach (string typeName in typeNames)
102+
{
103+
Type type = Type.GetType(typeName);
104+
if (type != null)
105+
{
106+
pageTypesList.Add(type);
107+
}
108+
}
109+
110+
return pageTypesList;
111+
}
112+
}
113+
```

0 commit comments

Comments
 (0)