Builds on type of the ConstTypeArgs.Core library to provide const type arguments that allow you to use type parameters to pass char values to generics at compile-time. This provides an analog to type specialization in C++, and can be used for scenarios such as: * Static configuration, * Eliminating unnecessary instance constructors, * "Passing" values to type initializers, * And more. Built-in const type arguments cover all ASCII & extended ASCII characters. Here's a simple demonstration showing how to define and use const type arguments and domain-specific type arguments: using ConstTypeArgs.Chars; // Const type arguments: public readonly struct Comma : K_Byte<Comma> { public static char Value => ','; } public readonly struct Semicolon : K_Byte<Semicolon> { public static char Value => ';'; } public readonly struct Slash : K_Byte<Slash> { public static char Value => (char)47; } public readonly struct VerticalBar : K_Byte<VerticalBar> { public static char Value => '|'; } public abstract class DefaultSeparator : Char<Semicolon> { } // Usage: public static class Foo<TChar> where TChar: K_Char { private static readonly char Separator = TChar.Value; public static void Output(string[] items) { foreach (var item in items) Console.Write($"{item}{Separator}"); } } // Elsewhere: var helloWorld = new string[] { "Hello", "World!" }; Foo<Comma>.Output(helloWorld); // Output: Hello,World! Foo<Slash>.Output(helloWorld); // Output: Hello/World! Foo<VerticalBar>.Output(helloWorld); // Output: Hello|World! Foo<DefaultSeparator>.Output(helloWorld); // Output: Hello;World!
$ dotnet add package ConstTypeArgs.CharsConst type arguments (also called const type args) are types used to pass constant & static values to generics through type parameters. These values are available at compile-time and can be used in static contexts, such as static constructors, static fields, and static methods. This can provide enhanced type safety, compile-time polymorphism, performance improvements, and more.
You can use types in the ConstTypeArgs.Chars namespace for passing ASCII character values as const type arguments.
The following is a simple Hello World example to demonstrate how this works:
using ConstTypeArgs.Chars;
public static class Foo<TChar>
where TChar: K_Char
{
private static readonly char Separator = TChar.Value;
public static void Output(string[] items)
{
foreach (var item in items)
Console.Write($"{item}{Separator}");
}
}
// Elsewhere
var helloWorld = new string[] { "Hello", "World!" };
Foo<Comma>.Output(helloWorld); // Output: Hello,World!
Foo<Slash>.Output(helloWorld); // Output: Hello/World!
Foo<Semicolon>.Output(helloWorld); // Output: Hello;World!
Foo<VerticalBar>.Output(helloWorld); // Output: Hello|World!
You can create new domain-specific char const type arguments like so:
public sealed class ListSeparator : Char<Comma>;
public sealed class TableSeparator : Char<VerticalBar>;