How Records Can Be Modified Using the ‘with’ Keyword

Listen to this Post

Featured Image
Records are immutable by default, meaning once an instance is created, its properties cannot be changed. However, modification is still possible using the `with` keyword, which creates a new instance with updated values while leaving the original record intact.

How It Works

The `with` keyword allows for non-destructive mutation by generating a new record with specified changes. The original record remains unchanged and will be garbage-collected if no longer referenced.

Example in C

public record Person(string Name, int Age);

var originalPerson = new Person("Alice", 30);
var modifiedPerson = originalPerson with { Age = 31 };

Console.WriteLine(originalPerson); // Output: Person { Name = Alice, Age = 30 }
Console.WriteLine(modifiedPerson); // Output: Person { Name = Alice, Age = 31 }

You Should Know: Practical Usage in Linux, Windows, and Cybersecurity

1. Immutable Files in Linux

Just like records, some files in Linux are immutable. To modify them, you must create a new version.

 Make a file immutable (root required) 
sudo chattr +i /etc/important.conf

Verify immutability 
lsattr /etc/important.conf

To modify, remove immutability first 
sudo chattr -i /etc/important.conf 

2. PowerShell Cloning Objects (Similar to ‘with’)

PowerShell allows object cloning with modifications:

$original = [bash]@{ Name="Server1"; Status="Online" } 
$modified = $original | Select-Object , @{Name="Status"; Expression={"Offline"}} 

3. Secure File Handling in Cybersecurity

When dealing with logs or sensitive data, always work on copies to avoid tampering:

 Create a backup before editing 
cp /var/log/auth.log /tmp/auth.log.backup

Edit the backup safely 
nano /tmp/auth.log.backup 

4. Git – Non-Destructive Versioning

Git uses a similar concept by creating new commits instead of modifying history:

git commit --amend  Creates a new commit instead of modifying the old one 

What Undercode Say

Immutable structures enhance security by preventing unintended modifications. The `with` keyword in C mirrors best practices in cybersecurity—always work on copies rather than original data.

Related Commands & Tools

  • Linux: chattr, cp, `rsync`
  • Windows: icacls, Copy-Item, `robocopy`
  • Cybersecurity: `sha256sum` (verify integrity), `gpg` (encrypted copies)

Expected Output:

A clear understanding of immutable modifications in programming and how similar principles apply to system administration and cybersecurity.

(No relevant URLs found in the original post for further reading.)

References:

Reported By: Pavledavitkovic Did – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram