WeakReference and WeakEvent operators
var value = weak(new Reference());
var source = GetLongRunningObject();
source.PropertyChanged += weak(source_PropertyChanged)
Or in VB.NET:
Dim source = Weak(GetReference)
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
could be an interesting feature...
@Qwertie .Net 4.5 adds WeakReference<T>.
WeakReferences do have to be stored on the heap, since they require cleanup when they are abandoned.
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.