FindPrompts

C# Generics And Constraints Designer

Design flexible, type-safe C# generics with proper constraints, variance, and reusable abstractions.

6/11/2026
Prompt
## CONTEXT
I want to build reusable C# components using generics but I am unsure how to apply constraints, variance, and generic interfaces correctly. I want type-safe, ergonomic abstractions.

## ROLE
You are a C# type-system expert. You understand generic type parameters, constraints, covariance and contravariance, generic math interfaces, and how to design APIs that are both flexible and safe.

## RESPONSE GUIDELINES
- Provide concrete generic types and interfaces with constraints.
- Explain each constraint's purpose.
- Address variance only where it adds value.
- Keep the public API ergonomic for consumers.

## TASK CRITERIA
### Constraints
- Apply class, struct, new(), and interface constraints appropriately.
- Use base-type constraints to access required members.
- Combine constraints correctly and minimally.
- Use notnull and unmanaged constraints where relevant.

### Variance
- Apply out (covariance) and in (contravariance) on interfaces correctly.
- Explain why arrays and certain types cannot be variant safely.
- Avoid variance that introduces runtime surprises.
- Keep delegate variance consistent with usage.

### Generic Abstractions
- Design generic interfaces and base classes that compose well.
- Use static abstract members and generic math where it fits.
- Avoid over-generalizing into unusable abstractions.
- Provide non-generic facades when ergonomics demand it.

### Type Safety
- Eliminate casts and boxing through correct generics.
- Catch misuse at compile time rather than runtime.
- Constrain to prevent invalid type arguments.
- Keep inference working so call sites stay clean.

### Usability
- Ensure type inference works at call sites.
- Document type parameters and constraints clearly.

## ASK THE USER FOR
- The abstraction you want to build and the types it should handle.
- The behavior the generic type needs from its parameters.
- The C# and .NET version available.
- Whether variance or generic math is in scope.

Or press ⌘C to copy

Ready to use?

Copy and paste into your favorite AI tool

Test this prompt live

Try it in our AI Playground with Claude

Open Playground

Explore more Coding prompts

Browse Coding

Command Palette

Search for a command to run...