Home > Code Analysis > CodeRush Code Issues specific to extension methods

CodeRush Code Issues specific to extension methods

July 11th, 2011

Extension methods allow developers to expand existing types without having to sub-class, recompile or modify the original type. They were introduced as a feature of CSharp version 3.0 and Visual Basic version 9.0. Such methods are just like static methods invoked by using instance method syntax.

You can declare extension methods only in public static classes (C#) or Modules (VB). To declare an extension method in C#, you specify the keyword this as the first parameter of the public static method, for example:

public static class Extensions
{
  public static int ToInt32 (this string s)
  {
    return Int32.Parse(s);
  }
}

In Visual Basic you have to specify the Extension() attribute from the System.Runtime.CompilerServices namespace. The first parameter in an extension method definition specifies which data type the method extends, for example:

Public Module Extensions
  <Extension()>
  Public Function ToInt32(s As String) As Integer
    Return Int32.Parse(s)
  End Function
End Module

There are additional rules for syntax of declaring extension methods. CodeRush suggests a few code issues that allows you to correctly write them and check its syntax before compiling the code. All code issues are of the error type, because your code won’t compile if you see them.

Here they are:

Parameter modifier ‘this’ should be the first parameter of extension method

Cause:

An extension method’s first parameter must include ‘this’ modifier in CSharp.

Sample:

CodeRush Extension method 'this' parameter should be first

How to fix:

  • Make sure that the parameter with ‘this’ modifier is declared as the first parameter of the extension method:

CodeRush Parameter Modifier This Should Be The First Parameter Of Extension Method Fix

Extension method cannot have a parameter array used with ‘this’ modifier

Cause:

The first parameter to an extension method cannot be a parameter array.

Sample:

CodeRush Extension method cannot have parameter array

How to fix:

  • Make sure that the parameter with ‘this’ modifier is not declared as a param array:

CodeRush Extension Method Cannot Have A Parameter Array Used With This Modifier Fix

Extension method cannot have a pointer parameter used with ‘this’ modifier

Cause:

The first parameter to an extension method must not be of pointer type.

Sample:

CodeRush Extension method cannot have a pointer parameter

How to fix:

  • Remove the pointer specifier from the parameter with ‘this’ modifier:

CodeRush Extension Method Cannot Have A Pointer Parameter Used With This Modifier Fix

Extension method must be defined in a non-generic static class

Cause:

The class holding extension methods must be static and must not be generic.

Sample:

CodeRush Extension method must be defined in a non generic static class

How to fix:

  • Make the class non-generic, if appropriate:

CodeRush Extension Method Must Be Defined In A Non Generic Static Class Fix

Extension method must be defined in a top level static class

Cause:

The class holding extension methods must be top level and cannot be nested.

Sample:

CodeRush Extension method must be defined in a top level static class

How to fix:

  • Define the class that contains the extension method as a top-level class, e.g.,:

CodeRush Extension Method Must Be Defined In A Top Level Static Class Fix

There are could be other code issues, such as “Extension methods can be called only on instances values” or “A reference to System.Core assembly is missing for declaring an extension method”. However, it is very easy to write your own code issue by creating a new plug-in for DXCore. Read about creating your own code issue using the CodeIssue provider DXCore control to learn more.

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: any
Updated: Nov/19/2012
ID: C111

Similar Posts: