Code Issues specific to partial methods
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:
Sample #2:
How to fix:
- Remove the incorrect modifiers:
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:
How to fix:
- Completely remove the ‘out’ parameter:
- Do not mark the parameter as ‘out’:
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:
How to fix:
- Mark the class ‘partial’:
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:
How to fix:
- Remove the partial keyword: