I've read the docs for C# nullable reference types.
I'm unsure how to handle the case of the framework calling my methods.
A simple EF Core example:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new CustomerConfiguration()); // <--- CA1062
}
This throws a warning CA1062:
In externally visible method 'void DbContext.OnModelCreating(ModelBuilder builder)', validate parameter 'builder' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument. csharp(CA1062)
I assume the framework is not going to send me null, so I see no point in checking for null.
How do I handle this scenario, namely: the framework calls my methods with arguments that cannot be null?
EDIT: my question differs from the linked one in that it's generic and specifically asks about C# 8's NRT feature. I only chose a related example. Thanks anyway for all those who helped.
NullReferenceException
! – Jeroen Mostertprivate
and all the callers are trivially validated within the class itself. The framework is just written by human coders anyway, it's not infallible. I've yet to see the first code base where too much parameter validation was a bottleneck... – Jeroen Mostert