Leanpub Header

Skip to main content

Prompt Engineering for .NET Developers

A free, developer-friendly guide to prompt engineering in C# — no Python required. Learn how LLMs work, structure reliable prompts, and build real workflows (code review, test generation, docs) using Microsoft.Extensions.AI, LM Studio, OpenAI, or Azure AI Foundry. Seven chapters. Seven runnable .NET 10 projects. Zero hype. Just dotnet new console and working code.

Minimum price

Free!

$1.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

Most prompt engineering books are written in Python. This one is not. If you write C# every day and you want to build real AI-powered features into your .NET applications, this book is for you.

Prompt Engineering for C# Developers takes you from your first IChatClient.GetResponseAsync() call to production-ready workflows across seven focused chapters. No Python. No Conda. No hedging. Just dotnet new console and working code that runs against local models (LM Studio), OpenAI, or Azure AI Foundry — using the exact same C# interface.

You will learn:

  • How LLMs actually work — tokens, context windows, temperature — enough theory to write better prompts, not a transformer architecture lecture
  • The 5-part prompt anatomy and the two foundational principles that hold up across every model generation
  • Core techniques: zero-shot, few-shot, chain-of-thought (and why "think step-by-step" is now largely obsolete advice)
  • How to handle sycophancy — the silent failure mode where the model tells you what you want to hear
  • Structured JSON output with defensive parsing, streaming, and resilience patterns using Microsoft.Extensions.Resilience
  • Real developer workflows: code review, unit test generation, commit messages, and documentation — all as reusable service patterns

Every chapter ends with a runnable C# project. You build a PromptBuilder fluent class in Chapter 4 and carry it through to Chapter 7's DevToolkit — a menu-driven console app that does code review, test generation, commit messages, and doc generation from a single IChatClient.

The book targets .NET 10 and uses Microsoft.Extensions.AI (MEAI) throughout. Honest about limitations. Direct about tradeoffs. Written for developers who ship real software and have opinions about it.

By the end, you will have working patterns for the things developers actually do with AI — not the demos that look good on stage, but the code that survives your next PR review.

Share this book

Categories

License

Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License

Author

About the Author

Taswar Bhatti

Taswar Bhatti has been writing about .NET, C#, and cloud since 2009 — back when Silverlight was still a thing and everyone was arguing about whether jQuery counted as a real framework.

He is a Cloud Solution Architect at Microsoft, focused on AI, security, and helping enterprise customers ship things that actually work in production. He speaks at multiple conferences around the world (Sweden, Prague, Germany, Poland, Turkiye, UK) including one of his favorite at NDC Security Manchester ("Agents That Talk Back: MCP, ACP, A2A, Oh my!"), he also runs the 5 Azure Highlights Thursday newsletter on LinkedIn, and blogs at taswar.zeytinsoft.com — where posts range from "Law of Demeter with a girlfriend-contacts-list analogy" (2009) to "New models in Microsoft Foundry".

Taswar started giving talks at the Ottawa .NET Community, back when he was living in Canada and became a Microsoft MVP till 2022 when he joined Microsoft (Istanbul, Turkiye).

He wrote this book because he was tired of every good prompt engineering resource being in Python. And because — honestly — .NET developers deserve better than "just install Conda."

Contents

Table of Contents

Chapter 1 — The .NET Developer’s AI Landscape

  1. 1.1 Let’s Get the Elephant Out of the Room
  2. 1.2 What Is a Large Language Model, Actually?
  3. 1.3 Base LLMs vs Instruction-Tuned LLMs
  4. 1.4 The Cost Spectrum: Local → Cloud → Enterprise
  5. 1.5 The Microsoft AI Stack for .NET Developers
  6. 1.6 Why C# Is Great for AI Application Development
  7. 1.7 What Prompt Engineering Actually Is
  8. 1.8 A Note on Hallucinations (and Why You Should Care)
  9. 1.9 What This Book Covers (and What It Doesn’t)
  10. 1.10 Sneak Peek: What Your Code Will Look Like
  11. 1.11 Chapter Summary

Chapter 2 — Setting Up Your AI Development Environment

  1. 2.1 What We’re Building
  2. 2.2 Path A: Local with LM Studio (Free)
  3. 2.3 Path B: OpenAI API
  4. 2.4 Path C: Azure AI Foundry
  5. 2.5 Setting Up the .NET Project
  6. 2.6 The Provider-Switching Pattern
  7. 2.7 Running HelloAI
  8. 2.8 Practical: HelloAI — Your First LLM Call
  9. Chapter Summary

Chapter 3 — How LLMs Work (Just Enough Theory)

  1. 3.1 Why This Chapter Exists (and What It Deliberately Skips)
  2. 3.2 Tokens: The Currency of LLMs
  3. 3.3 The Context Window: Your Model’s Working Memory
  4. 3.4 Temperature and Sampling: Controlling Randomness
  5. 3.5 The Chat Message Structure
  6. 3.6 Other Parameters Worth Knowing
  7. 3.7 Why the Same Prompt Gives Different Results
  8. 3.8 Practical: Parameter Playground
  9. Chapter Summary

Chapter 4 — Anatomy of a Great Prompt

  1. 4.1 From Good Intentions to Reliable Outputs
  2. 4.2 The 5-Part Prompt Anatomy
  3. 4.3 The Two Foundational Principles
  4. 4.4 Prompt Templates in C#: From String Interpolation to PromptBuilder
  5. 4.5 The Iterative Prompt Loop
  6. 4.6 Practical: PromptBuilder — Your Code Review Assistant
  7. 4.7 Chapter Summary

Chapter 5 — Core Prompting Techniques

  1. 5.1 Zero-Shot Prompting — When a Single Instruction Is Enough
  2. 5.2 Few-Shot Prompting — Teaching by Example
  3. 5.3 Role Prompting — Persona Engineering for Specialist Output
  4. 5.4 Chain-of-Thought — From “Step-by-Step” to “Think Hard”
  5. 5.5 Self-Consistency — Majority Rules
  6. 5.6 Sycophancy — The Problem You Didn’t Know You Had
  7. 5.7 Rubric-Based Prompting — Forcing Objectivity
  8. 5.8 Constraint Prompting — Setting Limits
  9. 5.9 Brainstorming Patterns — Getting Options, Not Oracles
  10. 5.10 Practical: TechniqueBenchmark
  11. Chapter Summary

Chapter 6 — Structured Outputs and Advanced Patterns

  1. 6.1 The Output Problem
  2. 6.2 Structured JSON Output — From Text to Types
  3. 6.3 Defensive Parsing — When the Model Doesn’t Follow Instructions
  4. 6.4 Streaming — Output as It Arrives
  5. 6.5 Resilience Patterns — Building on Fundamentally Unreliable Components
  6. 6.6 The Model-as-Validator Pattern — Generate, Validate, Correct
  7. 6.7 Prompt Injection in Production — When User Content Is Hostile
  8. 6.8 Practical — DocumentSummaryService
  9. Chapter Summary

Chapter 7 — Prompt Patterns for Real Developer Workflows

  1. 7.1 Code Review — Actionable, Not Chatty
  2. 7.2 Unit Test Generation — From Signature to Test Suite
  3. 7.3 Commit Messages and PR Descriptions — From Diff to Words
  4. 7.4 Documentation Generation — XML Docs and README Sections
  5. 7.5 Practical: DevToolkit — One App, Four Workflows
  6. 7.6 What Comes Next
  7. Chapter Summary

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub