CuginiTech.Const 1.0.4

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

// Install CuginiTech.Const as a Cake Tool
#tool nuget:?package=CuginiTech.Const&version=1.0.4

CuginiTech.Const

Utility class to allow strings to be created in a class that are sandboxed from other string constants. Loosely to behave like enums but for strings.

Class StringConst<T>

StringConst is a simple class to make string constants that are isolated to the parent class. This allows strings to be made that behave more like enums.

StringConst example

public sealed class Dogs
{
    public static readonly StringConst<Dogs> Candy = "Candy";
    public static readonly StringConst<Dogs> Trudie = "Trudie";
    public static readonly StringConst<Dogs> Fifi = "Fifi";
}

public sealed class People
{
    public static readonly StringConst<People> Hostess = "Candy";
    public static readonly StringConst<People> Friend = "Trudie";
    public static readonly StringConst<People> StereoType = "Fifi";
}

Type parameter T Class for constants to belong to.

Operand implicit(string source)

Allows implicit casting of a string to a StringConst.

Implicit example

public sealed class Dogs
{
    public static readonly StringConst<Dogs> Candy = "Candy";
}

Parameter source StringConst to return as string.

Operand explicit(StringConst<T> source)

Allows user to explictly cast a StringConts to a string.

Explicit example

StringConst<Dogs> Candy = "Candy";
var x = (string) Candy;

Parameter source StringConst item to convert.

Operand ==(StringConst<T> lhs, StringConst<T> rhs)

Creates custom equals operator that takes into account value and type.

== example

StringConst<Dogs> Candy = "Candy";
StringConst<Dogs> Trudie = "Trudie";
if (Candy == Trudie)
{
    // Error
}

public static void MatchPerson(StringConst<People> option)
{
    if (People.Hostess == option)
    {
        Console.WriteLine($"You have found the hostess {option}");
        return;
    }

    if (People.Friend == option)
    {
        Console.WriteLine($"You have my friend {option}");
        return;
    }

    if (People.StereoType == option)
    {
        Console.WriteLine($"Default french female name {option}");
        return;
    }
    Console.WriteLine($"No match {option}");
}

MatchPerson(People.Hostess);
// You have found the hostess Candy
MatchPerson(People.Friend);
// You have my friend Trudie
MatchPerson(People.StereoType);
// Default french female name Fifi

Parameter lhs First value.

Parameter rhs Second value.

Returns bool True if values are same.

Operand !=(StringConst<T> lhs, StringConst<T> rhs)

Not equals operator. Does negative of equals operator.

!= example

StringConst<Dogs> Candy = "Candy";
StringConst<Dogs> Trudie = "Trudie";
if (Candy != Trudie)
{
    // Success
}

Parameter lhs First value.

Parameter rhs Second value.

Returns True if values differ.

Method Switch(StringConst<T> key)

Allow conversion to string from const. Const needs to be already declared as part of a parent class.

Switch example

string name = StringConst<Dogs>.Switch(Dogs.Candy);

Parameter key Item to convert.

Returns Matching const or null.

Method Switch(string key)

Allow conversion to const from string.

Switch example

StringConst<Dogs> value = StringConst<Dogs>.Switch("Fifi");

Parameter key Item to convert.

Returns Matching string or null.

Method GetValues(Boolean nonPublic)

GetValues returns all the StringConsts created in a class.

GetValues example

public static void ListDogs()
{
    foreach (var dog in StringConst<Dogs>.GetValues())
    {
        Console.WriteLine(dog);
    }
}

// Candy
// Trudie
// Fifi

Parameter nonPublic Include non public members, default is no.

Returns returns all the StringConsts created in a class.

Method EqualsString(string src)

Allows a StringConst to be compared to a string.

EqualsString example

 StringConst<Dogs> Candy = "Candy";
if (Candy.EqualsString("Candy"))
{
    // Success
}

Parameter src String to compare to.

Returns Whether it matches or not.

Method Equals(Object obj)

Called when comparing an object to this value.

Parameter obj Const to compare to.

Returns True if are same.

Method Equals(StringConst<T> stringConst)

Called when comparing one instance to another.

Parameter stringConst Const to compare to.

Returns True if are same.

Method GetHashCode()

Hash code generation.

Returns Hash based on string and referral type.

Method ToString()

Allow casting to a string.

Returns string String contents.

Class Switcher<TKEY, TVALUE>

The switcher class was added to replicate the switch statement. StringConsts can't be used in a switch statement. Although the class is developed to be used by StringConst, it is just a generic class and can be used for any other needs.

Switcher example

var ageSwitch = new Switcher<StringConst<Dogs>, int>()
    .Case(Dogs.Trudie, 13)
    .Case(Dogs.Candy, 8)
    .Case(Dogs.Fifi, 5)
    .Default(-1);

var age = ageSwitch.Switch(Dogs.Candy);
// age = 8

var placeSwitch = new Switcher<int, string>()
    .Case(1, "st")
    .Case(2, "nd")
    .Case(3, "rd")
    .Default("th");

var place = $"{position}{placeSwitch(position)}";
// Good till 21

// Function based switcher
var mathSwitcher = new Switcher<string, Func<int, int, int>>()
   .Case("*", (a, b) => a * b)
   .Case("+", (a, b) => a + b);

mathSwitcher.Switch("*")(3, 4)
// 12
mathSwitcher.Switch("+")(3, 4)
// 7

Type parameter TKEY Source type to switch on.

Type parameter TVALUE Return value when matched.

Method Case(TKEY key, TVALUE result)

Add a case statement with a key and resulting value.

Parameter key Look up value to use.

Parameter result Return value if matched.

Returns Self for chaining.

Method Default(TVALUE result)

Add a default result to return if key is not found.

Parameter result Value to return if search returns no match.

Returns Self for chaining.

Method Switch(TKEY key)

Switch method to use key for a lookup if found then return the value added by case if not then registered default value otherwise default R.

Parameter key Key to search on.

Returns Matching result if found or default value.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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 netcoreapp2.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.2

    • No dependencies.
  • .NETCoreApp 3.1

    • No dependencies.
  • .NETFramework 4.5.2

    • No dependencies.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.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
1.0.4 280 1/11/2023
1.0.2 367 10/26/2022
1.0.1 350 10/24/2022