Adds the partial modifier to a class, struct, record or interface, so its members can be spread over several declarations.
Arguments
None. The target is the type, by symbol.
Precondition
- The type is a class, struct, record, record struct or interface. Enums and delegates cannot be partial.
- The type is not already partial.
partial is added as the last modifier, immediately before the class, struct, record or interface keyword, where C# requires it.- A declaration with no modifiers gets
partial in front of its keyword, after its attributes, documentation comment and any comment above it. - A containing type is left alone: a nested type can be partial on its own.
Preserved
- The type's meaning: a single partial declaration compiles to the same type.
- Attributes, comments, documentation, type parameters and constraints.
Limitations
- No new part is created; adding members in another file is a separate step, such as Move Member to Another Partial File.
Error codes
| Code | Meaning |
|---|
already-partial | the type is already declared partial |
unsupported-type-kind | the type is an enum or a delegate |
Cases
·
Diffs show each file from before to after.
/*[*/ … /*]*/ marks a selection and /*^*/ a caret; the runner removes them before the refactoring runs.
#already-partial-rejected
Refuses a type that is already partial
refusal
Targetsymbol T:Shop.Order
Refusesalready-partial; every file is left unchanged
Order.csinput
| 1 | | namespace Shop; |
| 2 | | |
| 3 | | public partial class Order |
| 4 | | { |
| 5 | | public int Id { get; set; } |
| 6 | | } |
View fixture on GitHub
#enum-rejected
Refuses an enum, which cannot be partial
refusal
Targetsymbol T:Shop.Status
Refusesunsupported-type-kind; every file is left unchanged
Status.csinput
| 1 | | namespace Shop; |
| 2 | | |
| 3 | | public enum Status |
| 4 | | { |
| 5 | | Open, |
| 6 | | Closed, |
| 7 | | } |
View fixture on GitHub
#generic-struct
Adds partial to a generic readonly struct with a constraint
success
Targetsymbol T:Shop.Range`1
Range.csmodified
| 1 | 1 | | using System; |
| 2 | 2 | | |
| 3 | 3 | | namespace Shop; |
| 4 | 4 | | |
| 5 | | − | public readonly struct Range<T> where T : IComparable<T> |
| 5 | + | public readonly partial struct Range<T> where T : IComparable<T> |
| 6 | 6 | | { |
| 7 | 7 | | public Range(T low, T high) |
| 8 | 8 | | { |
| 9 | 9 | | Low = low; |
| 10 | 10 | | High = high; |
| 11 | 11 | | } |
| 12 | 12 | | |
| 13 | 13 | | public T Low { get; } |
| 14 | 14 | | |
| 15 | 15 | | public T High { get; } |
| 16 | 16 | | } |
View fixture on GitHub
#interface-implemented-in-another-file
Adds partial to an interface, leaving the class that implements it in another file unchanged
success
Targetsymbol T:Shop.IPricing
IPricing.csmodified
| 1 | 1 | | namespace Shop; |
| 2 | 2 | | |
| 3 | | − | internal interface IPricing |
| 3 | + | internal partial interface IPricing |
| 4 | 4 | | { |
| 5 | 5 | | decimal Price(string sku); |
| 6 | 6 | | } |
FlatPricing.csunchanged
| 1 | 1 | | namespace Shop; |
| 2 | 2 | | |
| 3 | 3 | | internal class FlatPricing : IPricing |
| 4 | 4 | | { |
| 5 | 5 | | public decimal Price(string sku) => 1m; |
| 6 | 6 | | } |
View fixture on GitHub
#nested-record
Adds partial to a nested record, leaving the containing class as it is
success
Targetsymbol T:Shop.Catalogue.Entry
Catalogue.csmodified
| 1 | 1 | | namespace Shop; |
| 2 | 2 | | |
| 3 | 3 | | public class Catalogue |
| 4 | 4 | | { |
| 5 | | − | public record Entry(string Sku, decimal Price); |
| 5 | + | public partial record Entry(string Sku, decimal Price); |
| 6 | 6 | | |
| 7 | 7 | | public Entry First() => new Entry("A1", 2m); |
| 8 | 8 | | } |
View fixture on GitHub
#no-modifiers-with-documentation
Adds partial to a class with no modifiers, after its documentation comment, attribute and comment
success
Targetsymbol T:Shop.Basket
Basket.csmodified
| 1 | 1 | | using System; |
| 2 | 2 | | |
| 3 | 3 | | namespace Shop |
| 4 | 4 | | { |
| 5 | 5 | | /// <summary>The items a customer is about to buy.</summary> |
| 6 | 6 | | [Serializable] |
| 7 | 7 | | // Kept small on purpose. |
| 8 | | − | class Basket |
| 8 | + | partial class Basket |
| 9 | 9 | | { |
| 10 | 10 | | public int Count; |
| 11 | 11 | | } |
| 12 | 12 | | } |
View fixture on GitHub
#sealed-class
Adds partial after the other modifiers of a sealed class
success
Targetsymbol T:Shop.Invoice
Invoice.csmodified
| 1 | 1 | | namespace Shop; |
| 2 | 2 | | |
| 3 | | − | public sealed class Invoice |
| 3 | + | public sealed partial class Invoice |
| 4 | 4 | | { |
| 5 | 5 | | public decimal Total { get; set; } |
| 6 | 6 | | } |
View fixture on GitHub