In C# 9, you can use the with keyword to create a new instance of a record type with some or all of its properties updated. If you want to create a new instance of a record type with some properties unchanged, you can use the with keyword and specify the values of the properties that you want to change. However, if you want to create a new instance of a record type with some properties ignored and others updated, you need to use a workaround.
One workaround is to create a new instance of the record type and update its properties one by one using the with keyword. For example:
public record Person(string Name, int Age) { public Person WithName(string name) => this with { Name = name }; public Person WithAge(int age) => this with { Age = age }; public Person WithoutName() => this with { Name = null }; } var person1 = new Person("John", 30); var person2 = person1.WithoutName().WithAge(35); In this example, the Person record type has three methods: WithName, WithAge, and WithoutName. The WithName and WithAge methods use the with keyword to create a new instance of the Person record type with the Name or Age property updated. The WithoutName method creates a new instance of the Person record type with the Name property set to null.
Another workaround is to create a new instance of the record type using its constructor and passing in the values of the properties that you want to update. For example:
var person1 = new Person("John", 30); var person2 = new Person(null, person1.Age); In this example, the person2 instance of the Person record type has the same Age as person1, but its Name property is set to null.
Note that both of these workarounds require you to write more code than if you could simply ignore specific fields when using the with keyword. However, they provide a way to achieve the same result.
How to exclude specific fields when using C# 9 record with initializer?
Description: Learn how to selectively ignore or exclude certain fields from being initialized when using the C# 9 record with syntax.
Code:
var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord with { PropertyToIgnore = null }; C# 9 record with syntax excluding multiple fields
Description: Explore how to exclude multiple fields from being updated when using the C# 9 record with syntax.
Code:
var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord with { Property1 = "NewValue1", Property2 = null }; Ignore fields during record update using C# 9 with syntax and conditional logic
Description: Learn how to conditionally exclude fields from being updated in a C# 9 record using the with syntax.
Code:
var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord with { PropertyToIgnore = condition ? null : "NewValue" }; Exclude specific fields from C# 9 record with syntax based on attribute
Description: Explore how to use attributes or custom logic to exclude fields from being updated in a C# 9 record using the with syntax.
Code:
public record MyRecord { public string Property1 { get; init; } [IgnoreUpdate] public string PropertyToIgnore { get; init; } } var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord with { PropertyToIgnore = "NewValue" }; // Ignored due to attribute C# 9 record with syntax excluding fields by default
Description: Learn how to configure C# 9 records to exclude specific fields by default when using the with syntax.
Code:
public record MyRecord { public string Property1 { get; init; } public string PropertyToIgnore { get; init; } = default!; } var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord with { PropertyToIgnore = "NewValue" }; // Ignored by default value Conditional field exclusion in C# 9 record with syntax based on field value
Description: Explore how to conditionally exclude fields from being updated in a C# 9 record using the with syntax based on the current field value.
Code:
var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord with { PropertyToIgnore = originalRecord.PropertyToIgnore != "Exclude" ? "NewValue" : null }; C# 9 record with syntax excluding fields using a method
Description: Find out how to use a method to exclude fields from being updated in a C# 9 record using the with syntax.
Code:
public record MyRecord { public string Property1 { get; init; } public string PropertyToIgnore { get; init; } public MyRecord WithoutPropertyToIgnore() => this with { PropertyToIgnore = null }; } var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord.WithoutPropertyToIgnore(); Exclude fields when using C# 9 record with syntax by type
Description: Learn how to exclude fields from being updated in a C# 9 record using the with syntax based on the field type.
Code:
var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord with { PropertyToIgnore = "NewValue" as object }; // Ignored due to type mismatch C# 9 record with syntax excluding fields with default values
Description: Explore how to exclude fields with default values from being updated in a C# 9 record using the with syntax.
Code:
public record MyRecord { public string Property1 { get; init; } public string PropertyToIgnore { get; init; } = null!; } var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord with { PropertyToIgnore = "NewValue" }; // Ignored by default value Ignore fields when using C# 9 record with syntax based on a condition
Description: Learn how to conditionally exclude fields from being updated in a C# 9 record using the with syntax based on a specific condition.
Code:
var originalRecord = new MyRecord("Value1", "Value2", "Value3"); var updatedRecord = originalRecord with { PropertyToIgnore = condition ? null : "NewValue" }; runtimeexception sql-date-functions dax vue.js events cobertura gd topshelf indexoutofrangeexception break