Home > Code Analysis > Code Issues specific to partial methods

Code Issues specific to partial methods

September 9th, 2011

A partial method declaration has two parts: the declaration itself and an implementation. Both parts of a partial method can be located in a single class or in different parts of a partial class. You can use partial methods in the code and implement them later if required. If you do not supply an implementation for a partial method, its signature is removed by the compiler.

There are several conditions that are applied to partial methods, such as:

  • Partial methods must be void
  • Signatures of both parts of partial methods must match
  • Access modifiers are not allowed for partial methods
  • Partial methods must be declared in partial classes
  • etc

You don’t have to remember all these conditions if you have the CodeRush code issues feature turned on. When the rule of the partial method declaration is violated, code issues will show you an error or a hint, and you can fix it before you compile the code. These code issues are:

Partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers

Cause:

If a partial method has an invalid modifier (e.g., virtual, abstract, override, new, sealed, extern, or an access modifier) you will see an error in the code editor, because partial methods cannot have these type of modifiers.

Sample #1:

CodeRush Partial method cannot have modifiers

Sample #2:

CodeRush Partial method cannot have access modifiers

How to fix:

  • Remove the incorrect modifiers:

CodeRush Partial Method Fixes

Partial method cannot have out parameters

Cause:

Out parameters are not allowed for partial methods. If a partial method is declared with ‘out’ parameters, you will see an error.

Sample:

CodeRush Partial method cannot have out parameters

How to fix:

  • Completely remove the ‘out’ parameter:

CodeRush Partial Method Fixes

  • Do not mark the parameter as ‘out’:

CodeRush Partial Method Cannot Have Out Parameters

Partial method must be declared within a partial class or partial struct

Cause:

Partial methods can only reside inside a partial class or a partial structure, otherwise, an error is shown.

Sample:

CodeRush Partial method must be declared within a partial class

How to fix:

  • Mark the class ‘partial’:

CodeRush Partial Method Fixes

Partial method has only single part

Cause:

When the partial method has only a single part without a declaration, it does not need to be declared as partial. This code issue has a hint type.

Sample:

CodeRush Partial method has only single part

How to fix:

  • Remove the partial keyword:

CodeRush Partial Method Has Only Single Part Fix

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

Similar Posts:

  1. No comments yet. Be the first and leave a comment!