Skip to content

Commit ddd980b

Browse files
committed
consolidate existing extension articles
Create a new article to include all the existing extension errors and warnings.
1 parent cfa07ca commit ddd980b

File tree

14 files changed

+271
-416
lines changed

14 files changed

+271
-416
lines changed

.openpublishing.redirection.csharp.json

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@
343343
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0260.md",
344344
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/partial-declarations"
345345
},
346+
{
347+
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1112.md",
348+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
349+
},
346350
{
347351
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1683.md",
348352
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/assembly-references"
@@ -1818,16 +1822,52 @@
18181822
"source_path_from_root": "/docs/csharp/misc/cs1020.md",
18191823
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution"
18201824
},
1825+
{
1826+
"source_path_from_root": "/docs/csharp/misc/cs1100.md",
1827+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
1828+
},
1829+
{
1830+
"source_path_from_root": "/docs/csharp/misc/cs1101.md",
1831+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
1832+
},
1833+
{
1834+
"source_path_from_root": "/docs/csharp/misc/cs1102.md",
1835+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
1836+
},
1837+
{
1838+
"source_path_from_root": "/docs/csharp/misc/cs1103.md",
1839+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
1840+
},
18211841
{
18221842
"source_path_from_root": "/docs/csharp/misc/cs1104.md",
18231843
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays"
18241844
},
1845+
{
1846+
"source_path_from_root": "/docs/csharp/misc/cs1105.md",
1847+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
1848+
},
1849+
{
1850+
"source_path_from_root": "/docs/csharp/misc/cs1106.md",
1851+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
1852+
},
18251853
{
18261854
"source_path_from_root": "/docs/csharp/misc/cs1007.md",
18271855
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution"
18281856
},
18291857
{
1830-
"source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1502.md",
1858+
"source_path_from_root": "/docs/csharp/misc/cs1109.md",
1859+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
1860+
},
1861+
{
1862+
"source_path_from_root": "/docs/csharp/misc/cs1110.md",
1863+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
1864+
},
1865+
{
1866+
"source_path_from_root": "/docs/csharp/misc/cs1113.md",
1867+
"redirect_url": "/dotnet/csharp/language-reference/compiler-messages/extension-declarations"
1868+
},
1869+
{
1870+
"source_path_from_root": "/docs/csharp/misc/cs1502.md",
18311871
"redirect_url": "/dotnet/csharp/misc/cs1503"
18321872
},
18331873
{

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

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
---
2+
title: "Errors and warnings related to extension declarations"
3+
description: "These errors and warnings indicate that you need to modify the declaration of an extension method using the `this` modifier on the first parameter, or an extension declaration"
4+
ms.date: 05/23/2025
5+
f1_keywords:
6+
- "CS1100"
7+
- "CS1101"
8+
- "CS1102"
9+
- "CS1103"
10+
- "CS1105"
11+
- "CS1106"
12+
- "CS1109"
13+
- "CS1110"
14+
- "CS1112"
15+
- "CS1113"
16+
helpviewer_keywords:
17+
- "CS1100"
18+
- "CS1101"
19+
- "CS1102"
20+
- "CS1103"
21+
- "CS1105"
22+
- "CS1106"
23+
- "CS1109"
24+
- "CS1110"
25+
- "CS1112"
26+
- "CS1113"
27+
---
28+
# Errors and warnings related to extension methods declared with `this` parameters or `extension` blocks
29+
30+
- **CS1112**: *Do not use '<xref:System.Runtime.CompilerServices.ExtensionAttribute>'. Use the '`this`' keyword instead.*
31+
32+
## CS1112
33+
34+
This error is generated when the <xref:System.Runtime.CompilerServices.ExtensionAttribute> is used on a non-static class that contains extension methods. If this attribute is used on a static class, another error, such as CS0708: "Cannot declare instance members in a static class," might occur.
35+
36+
In C#, extension methods must be defined in a static class and the first parameter of the method is modified with the `this` keyword. Do not use the attribute at all in the source code. For more information, see [Extension Methods](../../../programming-guide/classes-and-structs/extension-methods.md). To correct this error remove the attribute and apply the `this` modifier to the first parameter of the method. The following example generates CS1112:
37+
38+
```csharp
39+
// cs1112.cs
40+
[System.Runtime.CompilerServices.ExtensionAttribute] // CS1112
41+
public class Extensions
42+
{
43+
public bool A(bool b) { return b; }
44+
}
45+
46+
class A { }
47+
```
48+
49+
## Compiler Error CS1100
50+
51+
Method 'name' has a parameter modifier 'this' which is not on the first parameter. The `this` modifier is allowed only on the first parameter of a method, which indicates to the compiler that the method is an extension method. To correct this error remove the `this` modifier from all except the first parameter of the method. The following code generates CS1100 because a `this` parameter is modifying the second parameter:
52+
53+
```csharp
54+
// cs1100.cs
55+
static class Test
56+
{
57+
static void ExtMethod(int i, this Test c) // CS1100
58+
{
59+
}
60+
}
61+
```
62+
63+
## Compiler Error CS1101
64+
65+
The parameter modifier 'ref' cannot be used with 'this'.
66+
67+
When the `this` keyword modifies the first parameter of a static method, it signals to the compiler that the method is an extension method. With C# version 7.1 and below, no other modifiers are needed or allowed on the first parameter of an extension method. Since C# version 7.2, `ref` extension methods are allowed, take a look at [extension methods](../programming-guide/classes-and-structs/extension-methods.md) for more details. The following example generates CS1101:
68+
69+
```csharp
70+
// cs1101.cs
71+
// Compile with: /target:library
72+
public static class Extensions
73+
{
74+
public static void Test(ref this int i) {} // CS1101
75+
}
76+
```
77+
78+
## Compiler Error CS1102
79+
80+
The parameter modifier 'out' cannot be used with 'this'.
81+
82+
When the `this` keyword modifies the first parameter of a static method, it signals to the compiler that the method is an extension method. No other modifiers are needed or allowed on the first parameter of an extension method.
83+
84+
To correct this error remove the unauthorized modifiers from the first parameter. The following example generates CS1102:
85+
86+
```csharp
87+
// cs1102.cs
88+
// Compile with: /target:library.
89+
public static class Extensions
90+
{
91+
// No type parameters.
92+
public static void Test(this out int i) {} // CS1102
93+
94+
//Single type parameter
95+
public static void Test<T>(this out T t) {}// CS1102
96+
97+
//Multiple type parameters
98+
public static void Test<T,U,V>(this out U u) {}// CS1102
99+
}
100+
```
101+
102+
## Compiler Error CS1103
103+
104+
The first parameter of an extension method cannot be of type 'type'.
105+
106+
The first parameter of an extension method cannot be a pointer type. The following example generates CS1103:
107+
108+
```csharp
109+
// cs1103.cs
110+
public static class Extensions
111+
{
112+
public unsafe static char* Test(this char* charP) { return charP; } // CS1103
113+
}
114+
```
115+
116+
## Compiler Error CS1105
117+
118+
Extension methods must be static.
119+
120+
Extension methods must be declared as static methods in a non-generic static class. The following example generates CS1105 because `Test` is not static:
121+
122+
```csharp
123+
// cs1105.cs
124+
// Compile with: /target:library
125+
public class Extensions
126+
{
127+
128+
// Single type parameter.
129+
public void Test<T>(this System.String s) {} //CS1105
130+
131+
}
132+
```
133+
134+
## Compiler Error CS1106
135+
136+
Extension methods must be defined in a non generic static class.
137+
138+
Extension methods must be defined as static methods in a non-generic static class. The following example generates CS1106:
139+
140+
```csharp
141+
// CS1106.cs
142+
public class NonStaticClass // CS1106
143+
{
144+
public static void ExtensionMethod1(this int num) {}
145+
}
146+
147+
public static class StaticGenericClass<T> // CS1106
148+
{
149+
public static void ExtensionMethod2(this int num) {}
150+
}
151+
152+
public static class StaticClass // OK
153+
{
154+
public static void ExtensionMethod3(this int num) {}
155+
}
156+
```
157+
158+
## Compiler Error CS1109
159+
160+
Extension Methods must be defined on top level static classes, 'name' is a nested class.
161+
162+
Extension methods cannot be defined in nested classes. The following example generates CS1109 because the class `Extension` is nested inside the class `Out`:
163+
164+
```csharp
165+
// cs1109.cs
166+
public class Test
167+
{
168+
}
169+
static class Out
170+
{
171+
static class Extension
172+
{
173+
static void ExtMethod(this Test c) // CS1109
174+
{
175+
}
176+
}
177+
}
178+
```
179+
180+
## Compiler Error CS1110
181+
182+
Cannot use 'this' modifier on first parameter of method declaration without a reference to System.Core.dll. Add a reference to System.Core.dll or remove 'this' modifier from the method declaration.
183+
184+
Extension methods are supported on version 3.5 and later of .NET Framework. Extension methods generate metadata which marks the method with an attribute. The attribute class is in system.core.dll. To correct this error, as the message states, add a reference to System.Core.dll or remove the `this` modifier from the method declaration. The following example generates CS1110 if the file is not compiled with a reference to System.Core.dll:
185+
186+
```csharp
187+
// cs1110.cs
188+
// CS1110
189+
// Compile with: /target:library
190+
public static class Extensions
191+
{
192+
public static bool Test(this bool b) { return b; }
193+
}
194+
```
195+
196+
## Compiler Error CS1113
197+
198+
Extension methods 'name' defined on value type 'name' cannot be used to create delegates.
199+
200+
Extension methods that are defined for class types can be used to create delegates. Extension methods that are defined for value types cannot. To correct this error, associate the extension method with a class type or make the method a regular method on the struct.
201+
202+
The following example generates CS1113:
203+
204+
```csharp
205+
// cs1113.cs
206+
using System;
207+
public static class Extensions
208+
{
209+
public static S ExtMethod(this S s)
210+
{
211+
return s;
212+
}
213+
}
214+
215+
public struct S
216+
{
217+
}
218+
219+
public class Test
220+
{
221+
static int Main()
222+
{
223+
Func<S> f = new S().ExtMethod; // CS1113
224+
return 1;
225+
}
226+
}
227+
```

docs/csharp/language-reference/compiler-messages/string-literal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ That's by design. The text closely matches the text of the compiler error / warn
2020

2121
- [**CS9274**](#constant-declarations): *Cannot emit this string literal into the data section because it has XXHash128 collision with another string literal.*
2222

23-
These errors indicate that your declaration can't be emitted in the data section. Turn this feature off for your application.
23+
These errors indicate that your declaration can't be emitted in the data section. Turn this feature off for your application.

docs/csharp/language-reference/toc.yml

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ items:
505505
CS1622, CS1624, CS1625, CS1626, CS1627, CS1629, CS1631, CS1637, CS4013, CS8154, CS8176, CS9237, CS9238, CS9239
506506
- name: Extension declarations
507507
href: ./compiler-messages/extension-declarations.md
508-
displayName:
508+
displayName: CS1100, CS1101, CS1102, CS1103, CS1105, CS1106, CS1109, CS1110, CS1113, CS1112
509509
- name: Partial declarations
510510
href: ./compiler-messages/partial-declarations.md
511511
displayName: >
@@ -530,7 +530,7 @@ items:
530530
href: ./compiler-messages/pattern-matching-warnings.md
531531
displayName: CS8509, CS9134, CS9135
532532
- name: String literal declarations
533-
href: ./compiler-messages/string-literal-errors.md
533+
href: ./compiler-messages/string-literal.md
534534
displayName: CS9274
535535
- name: Array declarations
536536
href: ./compiler-messages/array-declaration-errors.md
@@ -1473,30 +1473,10 @@ items:
14731473
href: ./compiler-messages/cs1061.md
14741474
- name: CS1065
14751475
href: ./compiler-messages/cs1065.md
1476-
- name: CS1100
1477-
href: ../misc/cs1100.md
1478-
- name: CS1101
1479-
href: ../misc/cs1101.md
1480-
- name: CS1102
1481-
href: ../misc/cs1102.md
1482-
- name: CS1103
1483-
href: ../misc/cs1103.md
1484-
- name: CS1105
1485-
href: ../misc/cs1105.md
1486-
- name: CS1106
1487-
href: ../misc/cs1106.md
14881476
- name: CS1107
14891477
href: ../misc/cs1107.md
14901478
- name: CS1108
14911479
href: ../misc/cs1108.md
1492-
- name: CS1109
1493-
href: ../misc/cs1109.md
1494-
- name: CS1110
1495-
href: ../misc/cs1110.md
1496-
- name: CS1112
1497-
href: ./compiler-messages/cs1112.md
1498-
- name: CS1113
1499-
href: ../misc/cs1113.md
15001480
- name: CS1502
15011481
href: ../misc/cs1503.md
15021482
- name: CS1503

0 commit comments

Comments
 (0)