So there was a class with an existing constructor with two fields. I do not want to interfere with the existing constructor:
//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.
Prasad RaveendranPosted Sep 20, 2023, 7:21 PM
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.
Tharpa RobertsPosted Sep 18, 2023, 8:28 PM
@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 RaveendranPosted Sep 16, 2023, 12:21 PM
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:
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.
Amit MohantyPosted Sep 15, 2023, 7:36 AM
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.