senrecep/csharpessentials/csharpessentials-any

Claude Code and agent skill by senrecep/csharpessentials

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Use when a method can return one of several distinct types. Covers Any<T0,T1> through Any<T0,…,T7> as a type-safe discriminated union, implicit assignment from any branch type, Match()/Switch() with one handler per branch, IsFirst/GetFirst, Is/As/TryAs, and Partition/Traverse for sequences.

Install

npx vskill@latest install senrecep/csharpessentials/csharpessentials-any

SKILL.md

CSharpEssentials.Any

Any<T0,T1,...> is a discriminated union: a value that is exactly one of several possible types at runtime. Replaces object-typed returns and eliminates unsafe casting.

Installation

bash
dotnet add package CSharpEssentials.Any

Namespace

csharp
using CSharpEssentials.Any;

Creating Any

csharp
// Implicit assignment from any branch type
Any<User, NotFoundError> found = user;
Any<User, NotFoundError> missing = new NotFoundError("User not found");
// Explicit factories: First, Second, ... (unions of 2 to 8 types)
Any<Order, ValidationFailure, NotFoundError> outcome = Any<Order, ValidationFailure, NotFoundError>.First(order);
// Or spell the type arguments on Any.Create
Any<User, NotFoundError> viaCreate = Any.Create<User, NotFoundError>(user);

Two type arguments of the same type (Any<int, int>) make the implicit conversion ambiguous (CS0457). Index is the active branch (0 for the first type) and Value its object? value.

Match and Switch

csharp
// Match returns AnyActionResult<TResult>: Status says whether a handler ran, Result holds its value.
// Handlers are optional parameters, so a missing handler returns Status = NotExecuted.
AnyActionResult<IResult> response = found.Match(
first: u => Results.Ok(u),
second: err => Results.NotFound(err.Message));
IResult http = response.Result!;
// Match and Switch throw InvalidOperationException on default(Any<...>) or a union holding null
// Switch runs side effects and returns AnyActionStatus
AnyActionStatus status = found.Switch(
first: u => Console.WriteLine(u.Name),
second: err => Console.WriteLine(err.Message));

Type Inspection

csharp
if (found.IsFirst)
{
User u = found.GetFirst(); // throws InvalidOperationException for the wrong branch
}
// Generic helpers take the target type followed by the union's type arguments.
// They test the runtime type of the value; for a value-type target As returns default (0 for int), not null, on a mismatch
bool isUser = found.Is<User, User, NotFoundError>();
User? asUser = found.As<User, User, NotFoundError>();
bool ok = found.TryAs<NotFoundError, User, NotFoundError>(out NotFoundError? error);
// Deconstruct / ToTuple: the inactive branches are default
var (maybeUser, maybeError) = found;

Collections

csharp
// Split a sequence of unions into one typed array per branch
(User[] users, NotFoundError[] notFound) = lookups.Partition();
// Or project and split in one pass
(User[] loaded, NotFoundError[] failed) = ids.Traverse(id => FindUser(id));

Typical Usage: service return type

csharp
public Any<Order, ValidationFailure, NotFoundError> PlaceOrder(PlaceOrderRequest request)
{
if (request.Quantity <= 0)
return new ValidationFailure("Quantity must be positive");
Cart? cart = _repo.FindCart(request.UserId);
if (cart is null)
return new NotFoundError("Cart not found");
return new Order { Id = Guid.NewGuid() };
}
// At the API boundary
public IResult PlaceOrderEndpoint(PlaceOrderRequest request) =>
PlaceOrder(request).Match(
first: order => Results.Created($"/orders/{order.Id}", order),
second: failure => Results.BadRequest(failure.Message),
third: err => Results.NotFound(err.Message)).Result!;

Best Practices

  • Use Any<T0,T1> over Result<T> when the error branches carry distinct, typed data
  • Pass a handler for every branch to Match()/Switch(); a missing handler is not a compile error, it returns NotExecuted
  • IsFirst/GetFirst() or TryAs<…>() are the escape hatch for cases where Match() is too verbose
  • Avoid object-typed union members; they defeat the purpose
  • A union serializes to JSON as {"Index":n,"Value":...} and deserializes back to the same branch

Security

  • Security scanPassed, 90 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

7-day trend score
0

Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/senrecep/csharpessentials/csharpessentials-any/badge)](https://verified-skill.com/skills/senrecep/csharpessentials/csharpessentials-any)

Something wrong with this skill or its page? Tell us.