-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Clarify CacheExtensions.TryGetValue<TItem> documentation for type mismatch behavior #11959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: jozkee <[email protected]>
|
Tagging subscribers to this area: @dotnet/area-extensions-caching |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR clarifies the documentation for CacheExtensions.TryGetValue<TItem> and CacheExtensions.Get<TItem> methods to explicitly explain their behavior when type mismatches occur.
Key changes:
- Updated method summaries to mention casting to
TItem - Clarified return values to explain both missing key and type mismatch scenarios
- Added comprehensive remarks explaining when to use non-generic alternatives
| <remarks>To be added.</remarks> | ||
| <c>true</c> if the key was found and the stored value can be cast to <typeparamref name="TItem" />; <c>false</c> otherwise.</returns> | ||
| <remarks> | ||
| This method returns <c>false</c> both when the key is not present in the cache and when the key is present but the stored value can't be cast to <typeparamref name="TItem" />. If you need to distinguish between these two cases (for example, in instrumented caches tracking hit/miss metrics), use the non-generic <see cref="M:Microsoft.Extensions.Caching.Memory.IMemoryCache.TryGetValue(System.Object,System.Object@)" /> method instead, which returns <c>true</c> if the key is present regardless of type. |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The remark sentence is too long and should be broken up for better readability. Consider splitting it into separate sentences to improve clarity and follow documentation best practices for conciseness.
| <remarks>To be added.</remarks> | ||
| <c>true</c> if the key was found and the stored value can be cast to <typeparamref name="TItem" />; <c>false</c> otherwise.</returns> | ||
| <remarks> | ||
| This method returns <c>false</c> both when the key is not present in the cache and when the key is present but the stored value can't be cast to <typeparamref name="TItem" />. If you need to distinguish between these two cases (for example, in instrumented caches tracking hit/miss metrics), use the non-generic <see cref="M:Microsoft.Extensions.Caching.Memory.IMemoryCache.TryGetValue(System.Object,System.Object@)" /> method instead, which returns <c>true</c> if the key is present regardless of type. |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the documentation guidelines, use a conversational tone with contractions. Change "is not" to "isn't" to be consistent with the use of "can't" elsewhere in the sentence.
| Gets the value associated with this key if present and castable to <typeparamref name="TItem" />. | ||
| </summary> | ||
| <returns>The value associated with this key, or <c>default(TItem)</c> if the key is not present.</returns> | ||
| <returns>The value associated with this key, or <c>default(TItem)</c> if the key is not present or the stored value can't be cast to <typeparamref name="TItem" />.</returns> |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the documentation guidelines, use a conversational tone with contractions. Change "is not present" to "isn't present" to be consistent with the use of "can't" elsewhere in the documentation.
| <param name="cache">The <see cref="T:Microsoft.Extensions.Caching.Memory.IMemoryCache" /> instance this method extends.</param> | ||
| <param name="key">The key of the value to get.</param> | ||
| <param name="value">The value associated with the given key.</param> | ||
| <param name="value">The value associated with the given key, or <c>default(TItem)</c> if the key is not found or the stored value can't be cast to <typeparamref name="TItem" />.</param> |
Copilot
AI
Dec 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the documentation guidelines, use a conversational tone with contractions. Change "is not found" to "isn't found" to be consistent with the use of "can't" elsewhere in the documentation.
Summary
Fixes the documentation ambiguity in
CacheExtensions.TryGetValue<TItem>andCacheExtensions.Get<TItem>to clarify that these methods handle type mismatches by returningfalse/default(TItem), not just when keys are absent.Problem
The existing documentation stated that
TryGetValue<TItem>returns "trueif the key was found;falseotherwise", which was ambiguous and misleading. In reality, the method returnsfalsein two distinct scenarios:TItemThis caused confusion, especially for instrumented cache decorators that track hit/miss metrics:
Changes
TryGetValue<TItem> Method
TItemtrueif the key was found and the stored value can be cast toTItem;falseotherwise"valueis set todefault(TItem)for both missing keys and type mismatchesfalsefor both missing keys and type mismatchesIMemoryCache.TryGetValuemethod, which returnstrueif the key exists regardless of typeGet<TItem> Method
TItem"default(TItem)is returned for both missing keys and type mismatchesResolution
Addresses #38969 by providing clear documentation that helps developers understand the type-checking behavior and guides them toward appropriate alternatives when they need to distinguish between "key not found" and "key found with incompatible type" scenarios.
Original prompt
Fixes #11958
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.