CLSS.Constants.ValueEquivalentStrings 1.1.1

dotnet add package CLSS.Constants.ValueEquivalentStrings --version 1.1.1
NuGet\Install-Package CLSS.Constants.ValueEquivalentStrings -Version 1.1.1
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="CLSS.Constants.ValueEquivalentStrings" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CLSS.Constants.ValueEquivalentStrings --version 1.1.1
#r "nuget: CLSS.Constants.ValueEquivalentStrings, 1.1.1"
#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.
// Install CLSS.Constants.ValueEquivalentStrings as a Cake Addin
#addin nuget:?package=CLSS.Constants.ValueEquivalentStrings&version=1.1.1

// Install CLSS.Constants.ValueEquivalentStrings as a Cake Tool
#tool nuget:?package=CLSS.Constants.ValueEquivalentStrings&version=1.1.1

CLSS.Constants.ValueEquivalentStrings

Problem

Among all value types in C#, most of them have equivalent default string representation (as returned by ToString()) if they have equivalent values. This is always the case for built-in value types, this is always the case for enum types, this is always the case for every struct types that can be found in the BCL. The only exception is custom struct types that have custom ToString override and equality conditions that don't align with each other. Those custom structs would create different string content for 2 struct instances that satisfy custom equality conditions.

Since string is a reference type, each ToString invocation always creates an allocation, but since strings are also immutable, there is no real benefit from 2 strings identical in content allocated on 2 different places on the heap. Such a scenario only creates memory inefficiency as well as garbage for the GC to clean up.

string Method1(int value) { return value.ToString(); }
string Method2(int value) { return value.ToString(); }

// These both create "4" strings on the heap at different places in a program
var res1 = Method1(4);
var res2 = Method2(4);

Solution

This package provides the ValueEquivalentStrings<T> class that contains a static, global cache of strings equivalent to value types. The first time it converts a value to a default string (meaning a ToString conversion with no additional format or format provider), it caches that string so that all subsequent conversions simply return that existing string.

int num1 = 4, num2 = 4;
string str1 = ValueEquivalentStrings<int>.Get(num1);
string str2 = ValueEquivalentStrings<int>.Get(num2); // no string allocation for this call

ValueEquivalentStrings accepts all value types in its type parameter, including custom struct types. If you want to cache string equivalences to custom struct types, make sure to align their ToString and Equals override implementations.

using CLSS;

public struct City
{
  public NameEnum Name;
  public CountryEnum Country;
  public static bool operator ==(City lhs, City rhs) => lhs.Equals(rhs);
  public static bool operator !=(City lhs, City rhs) => !lhs.Equals(rhs);
  public override string ToString() => $"(Name = {Name}, Country = {Country})";
  public override bool Equals(object obj)
  {
    if (obj is City otherCity) return this.Name == otherCity.Name;
    return false;
  }
}

// These 2 instances are treated as
// equivalent keys in the Dictionary backing ValueEquivalentStrings.
var paris1 = new City { Name = NameEnum.Paris, Country = CountryEnum.France };
var paris2 = new City { Name = NameEnum.Paris, Country = CountryEnum.USA };
var str1 = ValueEquivalentStrings<City>.Get(paris1);
var str2 = ValueEquivalentStrings<City>.Get(paris2); // returns "(Name = Paris, Country = France)"

At the heart of ValueEquivalentStrings<T> is a MemoizedFunc - another CLSS type and the only dependency of this package. This memoizer is also publicly exposed should you need to manipulate it directly for some reason.

// Invoking the memoizer directly does the same thing as Get, but longer syntax.
char charW = ValueEquivalentStrings<char>.Memoizer.Invoke('W');
// Clears the cache to free up some memory
ValueEquivalentStrings<int>.Memoizer.MemoizedResults.Clear();

This package also comes with the ToCachedString extension method for all value types as an alternative shorter, more functional-style syntax to Get. The same caution for custom struct types applies to this syntax.

// String.Join does not accept char as a separator type in pre-2.1 versions of .NET Standard
var commaSeparatedLine = String.Join(','.ToCachedString(), rowValues);

Note: Since ValueEquivalentStrings<T> is backed by a dictionary, all optimization rules to avoid boxing allocation for a dictionary key type - such as explicit underlying type for enum and implementing IEquatable<T> for struct - also apply to the type parameter of ValueEquivalentStrings<T>.

This package is a part of the C# Language Syntactic Sugar suite.
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. 
.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.0 is compatible.  netstandard1.1 was computed.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 was computed.  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. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wp8 was computed.  wp81 was computed.  wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 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
1.1.1 1,145 11/16/2022
1.1.0 1,226 8/13/2022
1.0.0 1,190 8/11/2022

- Added language-sensitive syntax highlighting in README file.