ktsu.SemanticString 1.4.0

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

SemanticString

A library that provides a transparent wrapper around system strings and gives you strong typing, compile time feedback, and runtime validation.

The intention is to be able to provide usage context and validation to naked strings in a similar way that Enums do for integers.

Usage

using ktsu.Semantics;

// Create a strong type by deriving a record class from SemanticString<TDerived>:
public record class MyStrongString : SemanticString<MyStrongString> { }

public class MyDemoClass
{
	public MyStrongString Stronk { get; set; } = new();

	public static void Demo(MyStrongString inStrongString, string inSystemString, out MyStrongString outStrongString, out string outSystemString)
	{
		// You can implicitly cast down to a System.String
		outSystemString = inStrongString;

		// You must explicitly cast up to a StrongString
		outStrongString = (MyStrongString)inSystemString;

		//You can provide a StrongString to a method that expects a System.String
		Path.Combine(inStrongString, inSystemString);

		// You can use the .WeakString property or the .ToString() method to get the value of the underlying System.String
		outSystemString = inStrongString.WeakString;
		outSystemString = inStrongString.ToString();

		// You can not implicitly cast up to a StrongString
		// outStrongString = inSystemString; // This will not compile

		// You can not cast from one StrongString to another
		// OtherStrongString other = inStrongString; // This will not compile
		// OtherStrongString other = (OtherStrongString)inStrongString; // This will not compile either
	}
}

Validation

You can provide custom validators which will throw a FormatException at runtime to help you catch data errors.

Implement the ktsu.Semantics.ISemanticStringValidator interface and provide it as a generic parameter when deriving your class:

public abstract class StartsWithHttp : ISemanticStringValidator
{
	public static bool IsValid(SemanticString? semanticString)
	{
		ArgumentNullException.ThrowIfNull(semanticString);

		return semanticString.StartsWith("http", StringComparison.InvariantCultureIgnoreCase);
	}
}

public abstract class EndsWithDotCom : ISemanticStringValidator
{
	public static bool IsValid(SemanticString? semanticString)
	{
		ArgumentNullException.ThrowIfNull(semanticString);

		return semanticString.EndsWith(".com", StringComparison.InvariantCultureIgnoreCase);
	}
}

public record class MyValidatedString : SemanticString<MyValidatedString, StartsWithHttp, EndsWithDotCom> { }
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ktsu.SemanticString:

Package Downloads
ktsu.PkmnDB

PkmnDB

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.1-pre.3 273 4/27/2025 1.4.1-pre.3 is deprecated because it is no longer maintained.
1.4.1-pre.2 68 4/26/2025
1.4.1-pre.1 133 4/4/2025
1.4.0 1,051 3/30/2025
1.3.5-pre.3 90 2/6/2025
1.3.5-pre.2 78 2/5/2025
1.3.5-pre.1 78 2/5/2025
1.3.4 428 12/27/2024
1.3.4-pre.28 78 2/4/2025
1.3.4-pre.27 73 2/3/2025
1.3.4-pre.26 80 2/1/2025
1.3.4-pre.25 76 1/30/2025
1.3.4-pre.24 69 1/28/2025
1.3.4-pre.23 72 1/26/2025
1.3.4-pre.22 74 1/24/2025
1.3.4-pre.21 73 1/22/2025
1.3.4-pre.20 75 1/20/2025
1.3.4-pre.19 67 1/18/2025
1.3.4-pre.18 67 1/16/2025
1.3.4-pre.17 55 1/14/2025
1.3.4-pre.16 66 1/13/2025
1.3.4-pre.15 75 1/11/2025
1.3.4-pre.14 67 1/10/2025
1.3.4-pre.13 69 1/10/2025
1.3.4-pre.12 66 1/8/2025
1.3.4-pre.11 73 1/7/2025
1.3.4-pre.10 80 1/6/2025
1.3.4-pre.9 90 1/4/2025
1.3.4-pre.8 76 1/3/2025
1.3.4-pre.7 78 1/3/2025
1.3.4-pre.6 78 1/3/2025
1.3.4-pre.5 83 1/2/2025
1.3.4-pre.4 92 12/31/2024
1.3.4-pre.3 73 12/29/2024
1.3.4-pre.2 75 12/28/2024
1.3.4-pre.1 77 12/27/2024
1.3.3-pre.1 79 12/27/2024
1.3.2-pre.1 71 12/27/2024
1.3.1-pre.1 76 12/27/2024
1.3.0 149 12/26/2024
1.2.18 128 12/26/2024

## v1.4.0 (minor)

Changes since v1.3.0:

- Add automation scripts for metadata generation and project management ([@matt-edmondson](https://github.com/matt-edmondson))
- Add LICENSE template ([@matt-edmondson](https://github.com/matt-edmondson))
- Fix analyzer violations ([@matt-edmondson](https://github.com/matt-edmondson))
- Renamed metadata files ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.3.4 (patch)

Changes since v1.3.4-pre.28:

- Add automation scripts for metadata generation and project management ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.3.0 (minor)

Changes since v1.2.18:

- Add new tests for SemanticString and MyValidatedString ([@matt-edmondson](https://github.com/matt-edmondson))
- Add SemanticStringExtensions for string conversions ([@matt-edmondson](https://github.com/matt-edmondson))
- Update binary content of LICENSE.md file ([@matt-edmondson](https://github.com/matt-edmondson))

## v1.2.18 (major)

Changes since 0.0.0.0:

- Initial commit ([@matt-edmondson](https://github.com/matt-edmondson))
- Refactor build scripts ([@matt-edmondson](https://github.com/matt-edmondson))
- Updated README.md ([@matt-edmondson](https://github.com/matt-edmondson))