ConstTypeArgs.Types
1.0.0
dotnet add package ConstTypeArgs.Types --version 1.0.0
NuGet\Install-Package ConstTypeArgs.Types -Version 1.0.0
<PackageReference Include="ConstTypeArgs.Types" Version="1.0.0" />
paket add ConstTypeArgs.Types --version 1.0.0
#r "nuget: ConstTypeArgs.Types, 1.0.0"
// Install ConstTypeArgs.Types as a Cake Addin #addin nuget:?package=ConstTypeArgs.Types&version=1.0.0 // Install ConstTypeArgs.Types as a Cake Tool #tool nuget:?package=ConstTypeArgs.Types&version=1.0.0
Type Const Type Arguments
Const 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.Types
namespace for passing Type values as const type arguments.
Usage scenarios include:
- Static configuration,
- Eliminating unnecessary instance constructors,
- "Passing" values to type initializers,
- and more.
Here's a simple demonstration:
using ConstTypeArgs.Types;
public class Foo<TType>
{
static Foo()
{
if (TType.Value == typeof(string)
|| TType.Value == typeof(char)
|| TType.Value == typeof(char[])
|| TType.Value == typeof(ReadOnlyMemory<char>)
|| TType.Value == typeof(ReadOnlySpan<char>))
{
Console.WriteLine($"{TType.Value} values usually represent text!");
}
if (TType.Value == typeof(byte)
|| TType.Value == typeof(sbyte)
|| TType.Value == typeof(short)
|| TType.Value == typeof(ushort)
|| TType.Value == typeof(int)
|| TType.Value == typeof(uint)
|| TType.Value == typeof(long)
|| TType.Value == typeof(ulong)
|| TType.Value == typeof(nint)
|| TType.Value == typeof(nuint))
{
Console.WriteLine($"{TType.Value} values usually represent numbers!");
}
}
}
// Elsewhere, assuming the value of StringType is typeof(string)
var foo = new Foo<StringType>(); // Outputs: StringType values usually represent text!
// Assuming that the value of IntType is typeof(int)
foo = new Foo<IntType>(); // Outputs: TType values usually represent numbers!
Here's how you could create a new Type const type argument:
public readonly struct StringType : K_Type<StringType>
{ public static Type Value => typeof(string); }
Product | Versions 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. |
-
net8.0
- ConstTypeArgs.Core (>= 1.0.0)
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.0.0 | 115 | 7/30/2024 |
Initial release.