© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•15mo ago•
14 replies
seky16

records, with expression, required property

I'm hitting a strange behaviour and not sure if it's intended.
I have the following record definition:
public record Options
{
    public required IDataSource DataSource { get; init; }

    public int? Take { get; init; } = null;
}
public record Options
{
    public required IDataSource DataSource { get; init; }

    public int? Take { get; init; } = null;
}

Then I use it in code like this:
var config = new Options() { DataSource = new DataSource(), }
assert(config.DataSource is null) // false
var config2 = config with { Take = null };
assert(config2.DataSource is null); // true
var config3 = config with { Take = null, DataSource = config.DataSource };
assert(config3.DataSource is null); // false
var config = new Options() { DataSource = new DataSource(), }
assert(config.DataSource is null) // false
var config2 = config with { Take = null };
assert(config2.DataSource is null); // true
var config3 = config with { Take = null, DataSource = config.DataSource };
assert(config3.DataSource is null); // false

Why is
config2.DataSource
config2.DataSource
null? Is this intended behaviour of combining
required
required
and
with
with
?
If so, shouldn't I get a warning/error that I have an uninitialised required property in
config2
config2
?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Required property
C#CC# / help
2y ago
❔ WPF binding expression of property from ItemsControl item with viewmodel property
C#CC# / help
4y ago
EF Entity 'required' property
C#CC# / help
2y ago
❔ EF Core: Owned property with converter expression cannot be converted
C#CC# / help
3y ago