With 6.0.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package With --version 6.0.0
                    
NuGet\Install-Package With -Version 6.0.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="With" Version="6.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="With" Version="6.0.0" />
                    
Directory.Packages.props
<PackageReference Include="With" />
                    
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 With --version 6.0.0
                    
#r "nuget: With, 6.0.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 With@6.0.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=With&version=6.0.0
                    
Install as a Cake Addin
#tool nuget:?package=With&version=6.0.0
                    
Install as a Cake Tool

with

With is a small library written in c# intended for alternative constructions in c# to do things that may look clumsy in regular code.

Why is this library small? Parts of the library has been removed as c# has evolved (and my understanding of what can be useful in c#).

What can we learn from "With"

Having access to expressions can help with doing extensions to a language in a relatively simple way.

Examples

Working with immutable data

If you need to get a copy of a readonly object but with some other value set in the new instance, you can use With. This is very similar to f# copy and update record expression. The main abstraction is called a lens. Lenses answers the question "How do you read and update immutable data". It may help to think about them as properties for immutable data that you can combine and compose. For further reading see the Basic lens operation part of the wiki

Simplest example
using With;
using With.Lenses;
...
public class CustomerNameChangeHandler
{
    // start with initializing the lens expression once (main cost is around parsing expressions)
    private static readonly DataLens<Customer, string> NameLens =
        LensBuilder<Customer>
            .Of(m => m.Name)
            .Build();
    public void Handle()
    {
        // fetch customer, say:
        var customer = new Customer(id:1, name:"Johan Testsson");
        // get a new instance of that customer but with changed name:
        var changedNameToErik = CustomerNameLens.Set(customer, "Erik Testsson");
        // ...
    }
}
Settings several properties at the same time
using System;
using With;
using With.Lenses;
...
public class CustomerChangeHandler
{
    // start with initializing the lens expression once (main cost is around parsing expressions)
    private static readonly DataLens<Customer, (int, string, IEnumerable<string>)> CustomerIdNamePreferencesLens =
        LensBuilder<Customer>
            .Of(m => m.Id)
            .And(m => m.Name)
            .And(m => m.Preferences)
            .Build();
    public void Handle()
    {
        // fetch customer, say:
        var customer = new Customer(id:1, name:"Johan Testsson");
        // get a new instance of that customer but with id, name and preferences changed:
        var change = CustomerIdNamePreferencesLens.Set(customer, (NextId(), "Erik Testsson", new []{"Swedish fish"}) );
        // ...
    }
}

Why shouldn't you use this library

The immutable data support in this library is done as an extensions to the language using the expression support in c#. A different way to add these things to c# would be to write some sort of roslyn extension in order to extend the language in a way that can be generated at compile time. This is done for instance in language ext codegen project. An approach such as that could be useful depending on your requirements.

On the .net platform there is already a language that allows you to write immutable first code in a terse and helpful way, f#, you can find out more on: f# for fun and profit. Many programmers prefer to work in c#/java why this library or codegen makes more sense.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.6 is compatible.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
6.1.0-beta-1 1,176 3/12/2020
6.0.0 4,214 12/25/2019
6.0.0-alpha-1 1,128 12/22/2019
5.1.4 1,429 12/22/2019
5.1.3 1,398 12/15/2019
5.1.2 2,271 11/22/2019
5.1.1 1,390 11/17/2019
5.1.0 1,277 11/16/2019
5.1.0-alpha-2 1,091 11/15/2019
5.1.0-alpha-1 1,102 11/13/2019
5.0.2 2,161 8/23/2018
5.0.1 1,669 8/23/2018
5.0.0 1,669 8/21/2018
4.3.1 2,259 4/11/2018
4.3.0 2,065 11/8/2017
4.2.0 1,826 11/2/2017
4.1.2 1,864 11/1/2017
4.1.1 1,806 10/3/2017
4.1.0 1,826 10/3/2017
4.0.0 1,786 9/18/2017
3.0.0 2,327 9/29/2016
2.0.0 2,147 6/11/2016
1.0.8 2,410 2/1/2016
1.0.7 1,996 10/12/2015
1.0.6 2,171 9/26/2015
1.0.5 2,055 9/25/2015
1.0.3 2,064 9/25/2015
1.0.2 2,020 9/24/2015
1.0.1 2,034 9/24/2015
1.0.0 2,078 9/18/2015
0.4.19 2,042 9/9/2015
0.4.18 2,006 8/19/2015
0.4.17 1,975 6/27/2015
0.4.16 2,092 3/22/2015
0.4.15 1,912 3/22/2015
0.4.14 1,937 3/21/2015
0.4.13 1,977 3/20/2015
0.4.12 1,938 3/20/2015
0.4.11 1,905 3/18/2015
0.4.10 1,922 3/18/2015
0.4.9 1,922 3/18/2015
0.4.8 1,885 3/12/2015
0.4.7 1,895 3/11/2015
0.4.6 1,979 3/11/2015
0.4.5 1,948 3/11/2015
0.4.4 1,905 3/10/2015
0.4.3 2,193 1/6/2015
0.4.2 2,705 12/19/2014
0.4.1 2,189 12/18/2014
0.4.0 2,184 12/18/2014
0.3.0 2,211 12/8/2014
0.2.0 2,396 11/16/2014
0.1.1 2,666 11/8/2014
0.1.0 2,414 11/6/2014
0.0.6 2,486 11/5/2014
0.0.5 2,236 11/3/2014
0.0.4 2,703 11/2/2014
0.0.3 2,079 10/20/2013
0.0.2 2,104 10/18/2013
0.0.1 1,990 10/17/2013

Trimmed down to what should be essential.