Why would a local string variable go from "not null" to "maybe null" when assigned to a dictionary?
I have this .NET 8.0 code that uses a Quartz.JobDataMap and gives me weird nullability warnings. I've narrowed it down to this object construction and assignment:
//cronExpression is a non-nullable string parameter
var test1 = cronExpression; //Tooltip says "'cronExpression' is not null here."
// Set up JobDataMap with necessary parameters
var jobDataMap = new JobDataMap
{
{ "JobName", dbJob.JobName },
{ "JobType", dbJob.JobType },
{ "CronExpression", cronExpression },
{ "Id", dbJob.Id }
};
var test2 = cronExpression; //Tooltip says "'cronExpression' may be null here."
I don't understand how this is possible. What can possibly trick the compiler into thinking this local variable can become null? And what's the best way to solve it and avoid the spurious nullability warnings down the line?