TruthyFalsey 1.0.2
dotnet add package TruthyFalsey --version 1.0.2
NuGet\Install-Package TruthyFalsey -Version 1.0.2
<PackageReference Include="TruthyFalsey" Version="1.0.2" />
<PackageVersion Include="TruthyFalsey" Version="1.0.2" />
<PackageReference Include="TruthyFalsey" />
paket add TruthyFalsey --version 1.0.2
#r "nuget: TruthyFalsey, 1.0.2"
#:package TruthyFalsey@1.0.2
#addin nuget:?package=TruthyFalsey&version=1.0.2
#tool nuget:?package=TruthyFalsey&version=1.0.2
Want to see what's changed?
TruthyFalsey
Slim .Net package giving you Truthy and Falsey evaluation in C# similar to JavaScript.
This project is inspired by Nick Chapsas' video on the topic on Youtube.
Installation
I recommend using the NuGet package: TruthyFalsey however feel free to clone the source instead if that suits your needs better.
Usage
Simply install the package in your project and you're good to go.
You'll now be able to evaluate truthy and falsey values in C# like this:
User? user = await GetUserAsync();
int userAge = user?.Age ?? 0;
if (user)
{
// User exists
}
if (!user)
{
// User is null
}
if (userAge)
{
// User age is different than 0
}
if (!userAge)
{
// User age is exactly 0
}
The logic behind the above is essentially: "is the value different to what would be the default for the type".
So for any reference types (class, record) it becomes a null check.
For value types (struct, enum, int, etc.) it becomes a comparison to the default value.
The intention here is to mimic how javascript does it.
All of the above is archived by overriding these 3 operators via extension methods:
public static bool operator true
public static bool operator false
public static bool operator !
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.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.