Constructor DI - Field is never assigned to, and will always have its default value

is never assigned to, and will always have its default value null unity
visual studio field is never assigned to
private field is never assigned
object reference not set to an instance of an object

I have the following code:

private IMemoryCache _cache;

public HomeController(IMemoryCache memoryCache) : base() {    
   this._cache = memoryCache;    
}

ASP.Net Core is handling the injection of the IMemoryCache instance via:

public void ConfigureServices(IServiceCollection services) {
   services.AddMemoryCache();
}

It is flagging the _cache field with:

Field HomeController._cache is never assigned to, and will always have its default value null

It's not causing any errors, it is just distracting in the editor.

What is the way to go about convincing Visual Studio 2017 that I am in fact assigning this field a value, via the constructor?

Code (because the error overlaps it):

With the editor showing the error:

At runtime showing the valid instance being injected:

Edit:

For the comments suggesting to use:

private IMemoryCache _cache = null;

... this makes no difference, same exact result.

Restarting Visual Studio 2017 and rebooting did not resolve the issue.

However, I was notified of Visual Studio 2017 update version 15.3.3 when the environment reloaded, which I installed.

After re-starting Visual Studio post-update, this situation is no longer occurring.

I doubt the update actually did anything related to this problem, it's more likely something in my VS configuration was off and the update put things back in place.

Field is never assigned to, and will always have its default value , Field is never assigned to, and will always have its default value. With the help of the smart people here, I have an object pooling script  public Address(string street, string zip, string city, Assignment_6_attempt_1.ContactFiles.Countries country)

I had the same case on VS2015. In my case Build -> Rebuild Solution resolved the issue.

Field is never assigned a non-default value - C# queries, // The type is potentially marshaled using an extern or interop. Fields are automatically given a default value when declared. If they are never assigned then they will always have their default value. A constructor with no parameters is called a default constructor. A default constructor has every instance of the class to be initialized to the same values. The default constructor initializes all numeric fields to zero and all string and object fields to null inside a class.

Cleaning your solution should solve this. In VS2017 menus: Project -> Clean Solution.

field is never assigned to and will always have its default, /// Default constructor for calling another constructor in this class. /// </summary>. public Address() : this(string.Empty, string.Empty, "Malmo"). {. Field name of field is never assigned to, and will always have its default value 'null' Oddly, when I check the inspector, those fields are not null. I have manually attached sprites to them.

Fix 'warning CS0649: is never assigned to, and will always have , There are some compilation warnings due to fields that are never assigned to is never assigned to, and will always have its default value null'  Field 'field' is never assigned to, and will always have its default value 'value'. The compiler detected an uninitialized private or internal field declaration that is never assigned a value.

C#, The default constructor initializes all numeric fields to zero and all string and object Geek.num' is never assigned to, and will always have its default value `0​'  The warning is 'Field grid_ is never assigned to, and will always have its default value null.' But that's simply wrong, since I do assign to it in initializeFromFile().

Upgrade to nullable reference types, You declare a variable to be a nullable reference type by appending a ? to the type. your design intent: some variables must always have a value, others may The assignment of Title and Uri to default which is null for the string type configures the AutoMapper so that it uses the constructor rather than  warning CS0649: Field 'wtf.handle' is never assigned to, and will always have its default value I understand what the compiler is telling me, but I do not understand why it does.

Comments
  • Okay where you are calling the HomeController constructor with IMemoryCache parameter.
  • You could assign null when it is declared private IMemoryCache _cache = null this should remove the warning
  • I seriously doubt the code shown in the post can trigger this warning. Please make sure what you have as sample of HomeController is actually minimal reproducible example for the error (and it should have absolutely nothing to do with DI).
  • @AlexeiLevenkov Screenshots added, because I wouldn't believe it either.
  • Do you have any other constructors overloads defined that don't assign _cache?