-
Notifications
You must be signed in to change notification settings - Fork 274
Add AV1030: Know when to use a record and when to use a class #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| rule_id: 1030 | ||
| rule_category: class-design | ||
| title: Know when to use a record and when to use a class | ||
| severity: 2 | ||
| --- | ||
| Records and classes serve different purposes. Choosing the right one keeps the intent of your type clear. This rule focuses on `record` (record class) vs `class`; for value types, consider `struct` or `record struct` instead. | ||
|
|
||
| Use a **record** when: | ||
| - The type represents data, not behavior (e.g. a DTO, a value object, or a response model). | ||
| - Structural equality (compare by value, not by reference) is the correct semantics. | ||
| - Immutability is desired (use `init`-only properties). | ||
|
|
||
| ```csharp | ||
| public record OrderSummary(Guid OrderId, decimal TotalAmount, DateTimeOffset CreatedAt); | ||
| ``` | ||
|
|
||
| **Note:** Record equality is shallow. If a record property is a collection (e.g. `List<T>` or an array), `Equals` and `GetHashCode` will not compare the contents, which can lead to subtle bugs. | ||
|
|
||
| Use a **record struct** when the same criteria apply, but you also want value-type semantics: stack allocation, no null, and copy-on-assignment. This is appropriate for small, frequently passed immutable data such as coordinates, colors, or date ranges. Avoid mutable `record struct` — mutations on a copy don't affect the original, which leads to confusing bugs. | ||
|
|
||
| Use a **class** when: | ||
| - The type has mutable state or significant behavior. | ||
| - Identity matters (two instances with the same data are not necessarily equal). | ||
| - The type manages a lifecycle (e.g. implements `IDisposable`). | ||
|
|
||
| ```csharp | ||
| public class OrderProcessor | ||
| { | ||
| public void Process(Order order) { ... } | ||
| } | ||
| ``` | ||
|
|
||
| Mixing both in one type (e.g. a record with complex behavior and mutable state) is a sign of a design problem. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.