|
| 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 | +``` |
0 commit comments