Prompting for C# Development

Overview

Prompt quality directly affects generated code quality. Strong prompts include context, constraints, and acceptance criteria.

Prompt Structure

Use a consistent format:

sequenceDiagram participant Dev as Developer participant AI as AI Assistant participant CI as CI Pipeline Dev->>AI: Provide task + context + constraints AI-->>Dev: Draft code and tests Dev->>CI: Run build and tests CI-->>Dev: Pass or fail feedback Dev->>AI: Request targeted fixes if needed

Practical Example - Good Prompt for API Handler

Task: Add idempotency support to POST /payments. Context: ASP.NET Core 9, MediatR, SQL Server, existing table Payments(Id, RequestId, Amount, Status). Constraints: Keep response schema unchanged. Reject duplicate RequestId with 200 and existing result. Acceptance: Unit tests for new and duplicate requests. No N+1 queries. Structured logs include request_id.

Expected output quality improves because the model has explicit boundaries.

C# Example - Contract and Handler Skeleton

public sealed record CreatePaymentCommand(string RequestId, decimal Amount) : IRequest<CreatePaymentResult>; public sealed record CreatePaymentResult(Guid PaymentId, string Status); public sealed class CreatePaymentHandler : IRequestHandler<CreatePaymentCommand, CreatePaymentResult> { public async Task<CreatePaymentResult> Handle(CreatePaymentCommand request, CancellationToken ct) { // Idempotency behavior belongs here: lookup by RequestId before insert. throw new NotImplementedException(); } }

Production Example - Prompt Templates in Repositories

Many teams keep prompt templates in version control under a folder such as docs/ai-prompts. Templates are reviewed like code and updated when architecture patterns change.

Common Pitfalls

Key Takeaways