Search code examples
c#.netcachinggarbage-collectionmemorycache

If I use the property of an object as the key in Memory Cache, will that object still be garbage collected?


Imagine you have an object like User, where one property is UserName. Say you want to use C# MemoryCache to store some information about that user, and use the UserName string property as the key in the cache.

Will this prevent garbage collection of the User object?


Solution

  • If I understand you correctly, you are saving the value of a property (string in this case) and not an object itself, so your object will be garbage collected.