I suggest you ...

WeakReference and WeakEvent operators

//weak reference:
var value = weak(new Reference());

//weak event:
var source = GetLongRunningObject();
source.PropertyChanged += weak(source_PropertyChanged)

Or in VB.NET:
'reference:
Dim source = Weak(GetReference)

'event:
Dim source = GetLongRunningObject();
AddWeakHandler source.PropertyChanged, AddressOf source_PropertyChanged

I would surely prefer a symbol operator rather than a verbose one, but I would prefer seeing it in the language

183 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    shimmyshimmy shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    4 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • supercat57supercat57 commented  ·   ·  Flag as inappropriate

        WeakReferences do have to be stored on the heap, since they require cleanup when they are abandoned.

      • QwertieQwertie commented  ·   ·  Flag as inappropriate

        Weak delegates would be extremely useful (not merely weak events) because the workarounds for weak delegates are often cumbersome. Most notably, transparently supporting weak events on the source side, i.e. making a weak event seem like a normal event, is impossible in partial-trust environments as it requires ReflectionPermission. Also, weak events tend to have significantly higher overhead than normal events.

        Only Microsoft, and no one else, can solve these problems by introducing a weak delegate type in which the method reference is strong and the Target is weak.

        This "weak()" operator suggestion seems really silly to me, but a new WeakReference<T> data type would be welcome; you can easily write a WeakReference<T> class yourself, but it requires the overhead of a cast. Plus, do weak references really have to be stored on the heap? It seems silly sometimes to use weak references to small classes, since the weak reference itself might be doubling the memory requirements.

      Feedback and Knowledge Base