Skip to content
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

ConditionalWeakTable return ref #26863

Closed
Thaina opened this issue Jul 19, 2018 · 2 comments
Closed

ConditionalWeakTable return ref #26863

Thaina opened this issue Jul 19, 2018 · 2 comments
Milestone

Comments

@Thaina
Copy link

Thaina commented Jul 19, 2018

As of now we have return ref in C#. So I wish we would have more of it support on collection. And ConditionalWeakTable is one I think needed

It could be new class such as ConditionalWeakTableRef<TKey,TValue>. Just have it return reference when index. So we could attach struct to hold extension value

@stephentoub
Copy link
Member

Returning ref from collections can be dangerous if the storage for that ref might get removed or replaced, e.g. if a ref is returned to an array in the a list, and then the list grows and replaces the array such that the ref is now effectively invalidated, pointing into an array no longer associated with the list. That could happen implicitly as part of a conditional weak table, with the data getting dropped automatically when the key is no longer kept alive. Plus conditional weak tables are often used in a concurrent environment, where the underlying storage could get invalidated by other threads interacting with the collection. In short, I don't think this is something we should do.

So we could attach struct to hold extension value

If your concern is being able to store a struct in a conditional weak table, you can just wrap it in an object, e,.g. instead of making it a ConditionalWeakTable<TKey, TValue>, make it a ConditionalWeakTable<TKey, StrongBox<TValue>>.

@Thaina
Copy link
Author

Thaina commented Jul 19, 2018

Understandable. Thank you for your clarification

@Thaina Thaina closed this as completed Jul 19, 2018
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants