loach.PureSharp 0.1.5

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

PureSharp

English | 日本語 | 简体中文 | 繁體中文 | Esperanto | Klingon | Español | Français | Deutsch | 한국어

CI & NuGet Upload NuGet License: MIT X (Twitter) Follow

PureSharp is a toolset designed to strongly support "referential transparency" and "immutability" in C#, bringing the safety of functional programming to C#. It leverages Roslyn analyzers to enforce robust, bug-resistant code writing at the compilation level.

Core Concepts

  1. Purity Enforcement: Explicitly declare logic without side effects and verify it mechanically.
  2. Immutability Introduction: Achieve "non-reassignable local variables," a feature lacking in the language specification, through intuitive naming conventions.
  3. Safe Control Flow: Provide pipeline-style conditional branching to prevent runtime exceptions and oversights.

Key Features

1. PureSharp.Core

Provides fundamental attributes and utilities.

  • [PureMethod] attribute: Declares a method as "pure" (referentially transparent).
  • Fluent.If: A fluent interface that allows if-else statements to be written as expressions.

2. PureSharp.Analyzers (Roslyn Analyzers)

Monitors code writing in real-time and reports rule violations.

Referential Transparency Verification (RTxxxx)

An Error is reported when a method annotated with [PureMethod] performs the following operations:

  • Accessing static, mutable (non-readonly) fields.
  • Calling non-pure methods (methods without [PureMethod]).
  • I/O operations (access to Console, File, Network, etc.).
Local Variable Immutability Enforcement (LVPxxxx)

Achieves immutability using a naming convention that starts with an underscore (_).

  • Mandatory Immutability: Prohibits reassignment to local variables starting with _ (Error).
  • Initialization Enforcement: Variables starting with _ must be initialized at the time of declaration (Error).
  • Naming Suggestion: Suggests adding an underscore (_) to variables that have never been reassigned (Warning).
FluentIf Termination Check (FIFxxxx)
  • Verifies that chains starting with Fluent.If are correctly terminated with .Else(). Failure to terminate will result in a compile error.

Quick Start

1. Writing Referentially Transparent Methods

using PureSharp.Core;

public class Calculator
{
    private static int _globalCache; // Mutable static field

    [PureMethod]
    public int Add(int a, int b)
    {
        // OK: Calculation only
        return a + b; 
        
        // NG: Accessing static field causes RT0001 error
        // _globalCache = a + b; 
        
        // NG: I/O operations cause RT0003 error
        // Console.WriteLine(a); 
    }
}

2. Utilizing Immutable Local Variables

public void Process()
{
    int _result = Calculate(); // Declared as an immutable variable
    
    // NG: Attempting to reassign causes LVP0001 error
    // _result = 10; 
    
    int count = 0; // Regular variable
    // Suggestion: If not reassigned, a warning to change to "_count" (LVP0003)
}

3. Safe Conditional Branching (FluentIf)

int status = Fluent.If(score >= 80, () => 1)
                   .ElseIf(score >= 60, () => 2)
                   .Else(0); // Forgetting .Else() causes FIF0001 error

Project Structure

  • PureSharp.Core: Provides core attributes and runtime libraries (.NET 10.0 / netstandard2.0).
  • PureSharp.Analyzers: The main Roslyn analyzer (netstandard2.0).
  • PureSharp.Analyzers.Tests: Unit tests to verify analyzer behavior (xUnit).

Motivation for Development

C# is a very powerful language, but in large-scale development or complex logic, debugging can become difficult due to unintended side effects or variable reuse. PureSharp was born to provide developers with "freedom (from bugs)" in the form of "constraints."


License

This project is released under the MIT License.

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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework 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 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.
  • .NETStandard 2.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
0.1.5 95 5/1/2026
0.1.4 123 4/3/2026
0.1.3 102 4/1/2026
0.1.2 106 3/31/2026
0.1.1 101 3/30/2026
0.1.0 259 3/28/2026