Home > Code Analysis > Code Issues specific to partial methods

Code Issues specific to partial methods

September 9th, 2011 Leave a comment Go to comments

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

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:
CodeRush Partial method cannot have modifiers
CodeRush Partial method cannot have access modifiers

Partial method cannot have out parameters

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

CodeRush Partial method cannot have out parameters

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

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

CodeRush Partial method must be declared within a partial class

Partial method has only single part

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:

CodeRush Partial method has only single part

—–
Products: CodeRush Pro
Versions: 11.1 and up
VS IDEs: any
Updated: Sep/09/2011
ID: C128

Similar Posts:

  1. No comments yet. Be the first and leave a comment!
  1. September 12th, 2011 at 04:12 | #1