Pledge 0.1.0-alpha.2

This is a prerelease version of Pledge.
dotnet add package Pledge --version 0.1.0-alpha.2
                    
NuGet\Install-Package Pledge -Version 0.1.0-alpha.2
                    
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="Pledge" Version="0.1.0-alpha.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pledge" Version="0.1.0-alpha.2" />
                    
Directory.Packages.props
<PackageReference Include="Pledge" />
                    
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 Pledge --version 0.1.0-alpha.2
                    
#r "nuget: Pledge, 0.1.0-alpha.2"
                    
#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 Pledge@0.1.0-alpha.2
                    
#: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=Pledge&version=0.1.0-alpha.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Pledge&version=0.1.0-alpha.2&prerelease
                    
Install as a Cake Tool

Pledge

Design by Contract for .NET 10+: preconditions, postconditions and Old() snapshots written as ordinary C# at the top of a method and checked at build time by a Roslyn source generator. No IL rewriter, no compiler fork, no IDE extension.

Alpha. The API can still change between pre-releases.

dotnet add package Pledge --prerelease

This package carries everything: the runtime, the generator, the analyzers and their code fixes, and the build targets that switch on the C# interceptors feature for you. There is no other setup.

Contracts

using PledgeContracts;

public decimal Withdraw(decimal amount)
{
    Pledge.Requires(amount > 0);
    Pledge.Requires(amount <= Balance, "insufficient funds");
    Pledge.Ensures(Balance == Pledge.Old(Balance) - amount);
    Pledge.Ensures(Pledge.Result<decimal>() >= 0);
    Pledge.EnsuresOnThrow<InvalidOperationException>(Balance == Pledge.Old(Balance));

    Balance -= amount;
    return Balance;
}

The generator checks the contracts at every call to the method in the same project, including async methods. A broken contract throws an exception that names the contract, the member and the caller:

Precondition of Account.Withdraw violated by its caller: insufficient funds [amount <= Balance] (at /src/Program.cs:9)

Modes

<PropertyGroup>
  <PledgeMode>Enforce</PledgeMode>
  <PledgePostconditions>Observe</PledgePostconditions>
</PropertyGroup>
Mode What happens
Ignore Nothing is checked and no checking code is generated. Default in Release.
Observe Violations go to PledgeRuntime.ViolationHandler and execution continues.
Enforce Violations throw PreconditionViolationException or PostconditionViolationException. Default in Debug.
EnforceFailFast Violations terminate the process after the handler runs.

PledgePreconditions and PledgePostconditions override the mode for one kind of contract.

Analyzers

The analyzers report misplaced contracts, misused Result and Old, impure contract expressions, Old snapshots of mutable objects, and constant arguments that break a precondition (Withdraw(-5)), and point out calls the contracts do not reach, such as a call through an interface. Each diagnostic links to its rule page.

Limits

Contracts are checked where the method is called, so they run for calls inside the project that declares them. Calls from other assemblies, through delegates, through an interface or base class, and by reflection are not checked yet. Contracts are supported on public and internal instance and static methods of classes, not yet on properties, constructors, generic methods or structs. The analyzers say when a contract will not run.

Property tests

Pledge.Testing runs the same contracts as property-based tests: generated inputs, preconditions as the filter, postconditions as the oracle.

Support

Pledge is MIT-licensed and built in the open. If it saves you a bug or two, consider sponsoring its development.

Source and docs · Design · Issues · License

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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Pledge:

Package Downloads
Pledge.Testing

Turns Pledge contracts into property-based tests: preconditions filter generated inputs, postconditions are the oracle. CsCheck backend.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0-alpha.2 0 10/30/2026
0.1.0-alpha.1 0 10/30/2026