So there was a class with an existing constructor with two fields. I do not want to interfere with the existing constructor:
C#
//existing
private IAuthDataProvider testAuthDBProvider;
private IPropertyProvider testPropertyProvider;
//I added
private IConnectorResolver testConnectorResolver;
private IAppContainerProvider testAppContainerProvider;
private IBlobStorageClient testBlobStorageClient;
//existing constructor
public TestAuthenticationProvider(IAuthDataProvider authDBProvider, propertyProvider)
{
testAuthDbProvider = authDbProvider;
testPropertyProvider = propertyProvider;
}
//I added this constructor
public TestAuthenticationProvider(IAuthDataProvider authDBProvider, propertyProvider, IConnectorResolver connectorResolver, IAppContainerProvider containerProvider, IBlobStorageClient blobStorageclient)
{
testAuthDbProvider = authDbProvider;
testPropertyProvider = propertyProvider;
testConnectorResolver = connectorResolver;
testAppContainerProvider = containerProvider;
testBlobStorageClient = blobStorageclient;
}And it seems to work okay. However, I now get a warning over the existing constructor:
Non-nullable field must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
But I can't declare it nullable. When I try I get an error that it must be non-nullable.

Amit MohantyRank #112 · 20K reputationPosted Sep 15, 2023, 3:36 AM EDT
When you declare a non-nullable reference type field or property, the compiler expects it to be initialized in the constructor. If it's not initialized in the constructor and the type is non-nullable, you'll get a warning. You can use the Nullable Reference Types feature to suppress the warning specifically for those fields.
You can use the #nullable disable and #nullable restore pragma directives to temporarily suppress the warning for those fields within the constructor.
#nullable disable public TestAuthenticationProvider(IAuthDataProvider authDBProvider, IPropertyProvider propertyProvider) { testAuthDBProvider = authDBProvider; testPropertyProvider = propertyProvider; } #nullable restorePrasad RaveendranRank #320 · 7.8K reputationPosted Sep 16, 2023, 8:21 AM EDT
The warning you're receiving is likely due to the fact that you have not declared the types of the propertyProvider parameter in both constructors. To resolve the warning and make the code more explicit, you should declare the type of propertyProvider. Assuming that propertyProvider is of type IPropertyProvider, your constructors should look like this:
// Existing constructor public TestAuthenticationProvider(IAuthDataProvider authDBProvider, IPropertyProvider propertyProvider) { testAuthDbProvider = authDBProvider; testPropertyProvider = propertyProvider; } // Added constructor public TestAuthenticationProvider(IAuthDataProvider authDBProvider, IPropertyProvider propertyProvider, IConnectorResolver connectorResolver, IAppContainerProvider containerProvider, IBlobStorageClient blobStorageclient) { testAuthDbProvider = authDBProvider; testPropertyProvider = propertyProvider; testConnectorResolver = connectorResolver; testAppContainerProvider = containerProvider; testBlobStorageClient = blobStorageclient; }The warning you mentioned, "Non-nullable field must contain a non-null value when exiting constructor," should now be resolved, assuming that all the fields and parameters have non-nullable types. If you're still encountering the warning after making this change, please ensure that you are not accidentally calling the old constructor without providing values for the new fields elsewhere in your code.
Tharpa Roberts22 reputationPosted Sep 18, 2023, 4:28 PM EDT
@Prasad
Unfortunately, that was just a transcription error. My actual code does declare the types of the propertyProvider parameter in both constructors. My actual code looks exactly like yours, but still gives the warning.
Prasad RaveendranRank #320 · 7.8K reputationPosted Sep 20, 2023, 3:21 PM EDT
If you're still encountering the warning after making this change, please ensure that you are not accidentally calling the old constructor without providing values for the new fields elsewhere in your code.
In addition to that.
Some IDEs and build tools allow you to configure code analysis rules. Check if there are any specific code analysis settings or extensions that might be affecting the warning.