TitanFx.WeakDelegate.Abstractions 1.1.0

dotnet add package TitanFx.WeakDelegate.Abstractions --version 1.1.0
                    
NuGet\Install-Package TitanFx.WeakDelegate.Abstractions -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TitanFx.WeakDelegate.Abstractions" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TitanFx.WeakDelegate.Abstractions" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="TitanFx.WeakDelegate.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TitanFx.WeakDelegate.Abstractions --version 1.1.0
                    
#r "nuget: TitanFx.WeakDelegate.Abstractions, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TitanFx.WeakDelegate.Abstractions@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TitanFx.WeakDelegate.Abstractions&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=TitanFx.WeakDelegate.Abstractions&version=1.1.0
                    
Install as a Cake Tool

TitanFx.WeakDelegate

A simple package which enables the easy creation of weak delegates. Weak delegates are delegates which dont hold onto a reference to the instance they were created from, making them perfect for use in places such as event handlers.

This packages primary purpose is to be used instead of the WeakEventManager pattern as it has much more boilerplate and requires no reflection.

Usage

There are only two real usages for this package:

Create a weak delegate from an existing delegate

using TitanFx.WeakDelegate;

// This delegate holds a reference to `myObject`
Action myDelegate = myObject.SomeMethod;

// This delegate holds a weak reference to `myObject` which allows it to be garbage collected
Action myWeakDelegate = myDelegate.ToWeakDelegate();

Create a new weak delegate directly

using TitanFx.WeakDelegate;

// This delegate holds a weak reference to `myObject` which allows it to be garbage collected
Action myWeakDelegate = Action.CreateWeakDelegate(myObject.SomeMethod);

Examples

Event handlers

A perfect use case for this is as an event handler, allowing the event source to call any handlers without preventing them from being garbage collected.


class MyModel : INotifyPropertyChanged
{
	
	private PropertyChangedEventHandler? _propertyChanged;
	public event PropertyChangedEventHandler? PropertyChanged
	{
		add => _propertyChanged += value?.ToWeakDelegate();
		remove => _propertyChanged -= value?.ToWeakDelegate();
	}

	public string Name
	{
		get;
		set
		{
			if (field != value)
			{
				field = value;
				_propertyChanged?.Invoke(this, new(nameof(Name)));
			}
		}
	}
}

class MyView
{
	private readonly MyModel _model;

	public MyView(MyModel model)
	{
		// Ordinarily this would cause a circular reference, meaning this view references the model
		// and the model references the view. This would prevent the view from being garbage collected
		// if the model was being referenced by something else and vice versa. However, because the model
		// registers its event handlers as weak handlers, in this situation the view holds a reference to
		// the model, but as soon as the view is no longer referenced by anything else it can be
		// successfully collected by the GC.

		_model = model;
		model.PropertyChanged += HandlePropertyChanged;
	}

	private void HandlePropertyChanged(object? sender, PropertyChangedEventArgs e)
	{
		StateHasChanged();
	}
}

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 140 6/8/2026
1.0.0 122 6/7/2026