C# Error CS1112 – Do not use ‘System.Runtime.CompilerServices.ExtensionAttribute’. Use the ‘this’ keyword instead.

C# Error

CS1112 – Do not use ‘System.Runtime.CompilerServices.ExtensionAttribute’. Use the ‘this’ keyword instead.

Reason for the Error & Solution

Do not use ‘System.Runtime.CompilerServices.ExtensionAttribute’. Use the ‘this’ keyword instead.

This error is generated when the 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.

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 .

To correct this error

  1. Remove the attribute and apply the this modifier to the first parameter of the method.

Example

The following example generates CS1112:

// cs1112.cs  
[System.Runtime.CompilerServices.ExtensionAttribute] // CS1112  
public class Extensions  
{  
    public bool A(bool b) { return b; }  
}  
  
class A { }

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...