Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions _rules/2225.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
rule_id: 2225
rule_category: dotnet-framework-usage
title: Use deconstruction to simplify variable assignments
severity: 3
---
C# supports deconstructing tuples, records, and any type that defines a `Deconstruct` method. Use this to avoid intermediate variables and make the intent of your code clearer.

Instead of:
Comment thread
dennisdoomen marked this conversation as resolved.

var point = GetOrigin();
Comment thread
dennisdoomen marked this conversation as resolved.
Outdated
var x = point.X;
var y = point.Y;

write:

var (x, y) = GetOrigin();

Similarly, deconstruct arrays and collections with pattern matching to avoid index-based access:

if (items is [var first, var second, ..])
{
// use first and second directly
}

**Tip:** Deconstruction also works in `foreach` loops:

foreach (var (key, value) in dictionary)
{
Console.WriteLine($"{key}: {value}");
}