Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungThe Model Got Better. Your Judgment Got Worse.(22.09.2026 um 03:02 Uhr)
Sichere ProgrammierungAIFeed - signed content permissions for AI web crawlers(22.09.2026 um 03:10 Uhr)
Sichere ProgrammierungThanks, glad you liked it!(22.09.2026 um 03:15 Uhr)
Sichere ProgrammierungA request for /.env shouldn't render your React app(22.09.2026 um 03:17 Uhr)
Sichere ProgrammierungAI-Agent Marketplaces Need Verifiable Delivery, Not More Listings(22.09.2026 um 03:20 Uhr)
AI & KI NachrichtenBeyond Bigger Models: Toward a Modular Cognitive Architecture(22.09.2026 um 03:21 Uhr)
Sichere ProgrammierungMasa Depan Manajemen Data: Mengenal Konsep Data Mesh yang Revolusioner(22.09.2026 um 03:22 Uhr)
Sichere ProgrammierungHow to Search Your Claude Code Conversation History(22.09.2026 um 03:22 Uhr)
Sichere ProgrammierungThe Model Got Better. Your Judgment Got Worse.(22.09.2026 um 03:02 Uhr)
Sichere ProgrammierungAIFeed - signed content permissions for AI web crawlers(22.09.2026 um 03:10 Uhr)
Sichere ProgrammierungThanks, glad you liked it!(22.09.2026 um 03:15 Uhr)
Sichere ProgrammierungA request for /.env shouldn't render your React app(22.09.2026 um 03:17 Uhr)
Sichere ProgrammierungAI-Agent Marketplaces Need Verifiable Delivery, Not More Listings(22.09.2026 um 03:20 Uhr)
AI & KI NachrichtenBeyond Bigger Models: Toward a Modular Cognitive Architecture(22.09.2026 um 03:21 Uhr)
Sichere ProgrammierungMasa Depan Manajemen Data: Mengenal Konsep Data Mesh yang Revolusioner(22.09.2026 um 03:22 Uhr)
Sichere ProgrammierungHow to Search Your Claude Code Conversation History(22.09.2026 um 03:22 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

c# advanced: Enhancing Records Adding Flexibility with Additional Members

Records in C# are a game-changer when it comes to defining data-centric types. They offer built-in features like value-based equality, immutability, and a concise syntax. While the primary constructor is great for defining essential…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Records in C# are a game-changer when it comes to defining data-centric types. They offer built-in features like value-based equality, immutability, and a concise syntax. While the primary constructor is great for defining essential properties, you might need to extend records with optional properties, methods, or derived values for more complex scenarios.



In this article, we’ll explore how to enhance records by adding additional members and discuss scenarios where they shine. We’ll include clear examples and practical assignments to deepen your understanding.









Why Extend a Record?



The primary constructor in a record defines the minimum information needed to create an instance. However, there are cases where you might need:





  • Derived Properties: Compute values based on existing properties.


  • Optional Fields: Handle data that isn’t always required during initialization.


  • Custom Methods: Add functionality specific to the record’s purpose.



C# allows records to be flexible by defining a body where you can add fields, properties, and methods.









Adding Derived Properties



Derived properties are useful when you need to compute values based on other properties. Let’s define a Book record with Title and Author as required fields. We’ll also add a computed property, DisplayTitle, that combines them for display purposes.




public record Book(string Title, string Author)
{
// Derived property
public string DisplayTitle => $"{Title} by {Author}";
}









Usage Example:






var book = new Book("The Great Gatsby", "F. Scott Fitzgerald");
Console.WriteLine(book.DisplayTitle); // Output: The Great Gatsby by F. Scott Fitzgerald












Adding Optional Properties with init



Optional properties aren’t always a part of the primary constructor. Using the init keyword, you can define properties that can only be set during initialization and remain immutable afterward.



Here’s an example with a Car record:




public record Car(string Make, string Model)
{
// Optional immutable property
public int? Year { get; init; }
}









Usage Example:






var car = new Car("Tesla", "Model S") { Year = 2021 };
Console.WriteLine($"{car.Make} {car.Model}, Year: {car.Year}");
// Output: Tesla Model S, Year: 2021

// Attempting to modify 'Year' after initialization will cause a compiler error:
// car.Year = 2022; // Error






The init keyword ensures that optional properties are immutable after being initialized.









Using Nested Records for Complex Data



Nested records are a powerful way to handle complex data. Let’s define a Customer record with a nested Address record:




public record Address(string Street, string City);

public record Customer(string Name)
{
// Optional property with a nested record
public Address? Address { get; init; }
}









Usage Example:






var customer = new Customer("Alice") 
{
Address = new Address("123 Elm Street", "Springfield")
};

Console.WriteLine($"{customer.Name} lives at {customer.Address?.Street}, {customer.Address?.City}");
// Output: Alice lives at 123 Elm Street, Springfield






The nested Address record integrates seamlessly, allowing for clean and concise data structures.









Adding Custom Methods



Custom methods let you add specific functionality to records. For instance, let’s add a method to a Transaction record to calculate the total cost:




public record Transaction(string Item, int Quantity, decimal UnitPrice)
{
// Method to calculate the total cost
public decimal CalculateTotal() => Quantity * UnitPrice;
}









Usage Example:






var transaction = new Transaction("Laptop", 2, 1500.00m);
Console.WriteLine(transaction.CalculateTotal()); // Output: 3000.00






This method encapsulates behavior directly within the record, keeping the logic close to the data.









Immutability with with Expressions



While records are immutable, you can create new instances with modified properties using the with expression. This is particularly useful when you want to update data while preserving the original object.



Let’s enhance the Customer record with this feature:




var originalCustomer = new Customer("Alice")
{
Address = new Address("123 Elm Street", "Springfield")
};

var updatedCustomer = originalCustomer with { Address = new Address("456 Oak Avenue", "Shelbyville") };

Console.WriteLine(originalCustomer.Address?.Street); // Output: 123 Elm Street
Console.WriteLine(updatedCustomer.Address?.Street); // Output: 456 Oak Avenue






The with expression ensures immutability while allowing flexibility for updates.









Assignments to Test Your Understanding






Easy Level




  1. Create a Movie record with:



    • Title and Director as primary constructor properties.

    • A computed property Description that combines both.





Example Output:




   var movie = new Movie("Inception", "Christopher Nolan");
Console.WriteLine(movie.Description); // Output: Inception by Christopher Nolan







  1. Add an optional property Rating using init to the Movie record.









Medium Level




  1. Define a Product record with:



    • Name and Price in the primary constructor.

    • A method ApplyDiscount(decimal percentage) that returns a new Product with the discounted price.





Example Output:




   var product = new Product("Laptop", 2000.00m);
var discountedProduct = product.ApplyDiscount(10);
Console.WriteLine(discountedProduct.Price); // Output: 1800.00







  1. Add a nested record Category to represent the product category, and integrate it into the Product record.









Difficult Level




  1. Create an Order record:


    • Include OrderId, ItemName, Quantity, and UnitPrice in the primary constructor.

    • Add a computed property TotalPrice that calculates the total.

    • Add a method UpdateQuantity(int newQuantity) that returns a new Order with the updated quantity.





Example Output:




   var order = new Order("ORD001", "Tablet", 2, 500.00m);
Console.WriteLine(order.TotalPrice); // Output: 1000.00

var updatedOrder = order.UpdateQuantity(5);
Console.WriteLine(updatedOrder.TotalPrice); // Output: 2500.00












When to Use Records vs. Classes





  • Use records for:




    • Immutable data-centric types.

    • Scenarios requiring value-based equality.

    • Clean and compact definitions.








  • Use classes for:




    • Types with significant behavior or mutable state.

    • Complex hierarchies and relationships.














Conclusion



Extending records with additional members, computed properties, and methods allows you to create versatile and maintainable data models. By leveraging features like the init keyword and with expressions, you can balance immutability and flexibility.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten c# advanced: Enhancing Records Adding Flexibility with Additional Members

Thematisch verwandte Begriffe: advanced, Enhancing, Records, Adding · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-49449 | Joplin is an open source note-taking and to-do application that organise…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick