Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 3 · LANGUAGES

How to Code in C#/.NET Using AI

Use an AI assistant to write a C# method, build it with the .NET SDK, and prove it with xUnit tests. Works in Visual Studio, VS Code or Rider.

In short: say the .NET version, use decimal for money, give the method signature and rules, ask for xUnit tests, run dotnet test.

Why C# with AI

WHY

C# runs a lot of business software: banking, insurance, retail and government apps. It has strong types and great test tools, so AI mistakes show up fast at build time.

What you need

WHAT
  • .NET SDK 10: the current long-term support version. Check with dotnet --version.
  • xUnit: a popular .NET test library. The xunit project template sets it up.
  • IDE + assistant: Visual Studio with Copilot, VS Code with C# Dev Kit + Copilot or Claude Code, or Rider.

How it flows

1 · Describe the method decimal, rules, example 2 · AI writes code read every line 3 · dotnet build no errors, no warnings 4 · Ask for xUnit tests edge cases 5 · dotnet test Passed = keep

Install and set up

HOW
  1. Install the .NET 10 SDK from dotnet.microsoft.com (or with the Visual Studio Installer).
  2. Check: dotnet --version shows 10.x.
  3. Create the projects:
dotnet new classlib -n Billing
dotnet new xunit -n Billing.Tests
dotnet add Billing.Tests reference Billing

Step 1 — the prompt

COPY THIS PROMPT

Write a C# static class Gst in namespace Billing (.NET 10) with Add(decimal price, decimal ratePercent) that returns a tuple (Gst, Total). Round GST to 2 decimals, away from zero. Throw ArgumentOutOfRangeException for a negative price or a rate outside 0–100. Example: Add(1000, 18) returns (180, 1180). Only return the code.

Step 2 — the code (tested here)

Billing/Gst.cs
// GST calculator. Built with an AI assistant, then tested.
namespace Billing;

public static class Gst
{
    /// <summary>Returns (gst, total) for a price before tax. Rounds to 2 decimals.</summary>
    public static (decimal Gst, decimal Total) Add(decimal price, decimal ratePercent)
    {
        if (price < 0)
            throw new ArgumentOutOfRangeException(nameof(price), "Price cannot be negative.");
        if (ratePercent < 0 || ratePercent > 100)
            throw new ArgumentOutOfRangeException(nameof(ratePercent), "Rate must be 0 to 100.");

        decimal gst = Math.Round(price * ratePercent / 100m, 2, MidpointRounding.AwayFromZero);
        return (gst, price + gst);
    }
}

Step 3 — ask for xUnit tests, then run them

FOLLOW-UP PROMPT

Write xUnit tests for Gst.Add: 18% on 1000, rounding on 100.25 at 5%, a bad rate, and a negative price.

Billing.Tests/GstTests.cs
using Billing;
using Xunit;

public class GstTests
{
    [Fact]
    public void Adds18Percent()
    {
        var (gst, total) = Gst.Add(1000m, 18m);
        Assert.Equal(180m, gst);
        Assert.Equal(1180m, total);
    }

    [Fact]
    public void RoundsToTwoDecimals() => Assert.Equal(5.01m, Gst.Add(100.25m, 5m).Gst);

    [Fact]
    public void RejectsBadRate() =>
        Assert.Throws<ArgumentOutOfRangeException>(() => Gst.Add(100m, 150m));

    [Fact]
    public void RejectsNegativePrice() =>
        Assert.Throws<ArgumentOutOfRangeException>(() => Gst.Add(-1m, 5m));
}
dotnet test
# -> Passed!  - Failed: 0, Passed: 4
Real catch while writing this: the first AI version only allowed the old GST slabs 5, 12, 18 and 28%. From 22 September 2025 India moved to mainly 5% and 18% (plus 40% for some luxury and sin goods), with a few special rates. AI can know old rules. We changed it to accept any rate from 0 to 100 and let the business pick the rate.

Settings and config

  1. Turn on nullable checks: <Nullable>enable</Nullable> in the .csproj (new templates do this). It catches AI null mistakes.
  2. Treat warnings as errors in CI: dotnet build -warnaserror.
  3. Put your rules (".NET 10, xUnit, decimal for money") in .github/copilot-instructions.md or CLAUDE.md.

Good C# prompts (reuse these)

  1. “Convert this class to a record with init-only properties.”
  2. “Make this method async and pass a CancellationToken.”
  3. “Add ILogger logging, but never log card numbers or passwords.”
  4. “Explain this exception and fix it:” + the top lines of the stack trace.

Watch out (common AI mistakes in C#)

  • double for money — ask for decimal.
  • Old rules and APIs — tax slabs, old .NET Framework code; say your version.
  • Made-up NuGet packages — check on nuget.org.
  • .Result / .Wait() on async code can deadlock; ask for await.

Quick quiz

1. Which type should you use for money in C#?

decimal.

2. How do you run xUnit tests from the terminal?

dotnet test.

3. What does Gst.Add(1000, 18) return?

(180, 1180).

4. What old fact did the AI get wrong in this chapter?

It used the old GST slabs (12% and 28%) that changed in September 2025.

Save tokens and money

SMART AI USE

Paste one class, not the whole solution or bin/obj folders. Paste only the first lines of a stack trace. Let the IDE do free refactors (rename, extract method) and ask the AI only for logic and tests.

Save these cards

INTERVIEW CARDAI Coding

How do you use AI safely for C#?

Give .NET version, signature, rules, example.
Build with nullable on; prove with xUnit.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

decimal vs double

decimal: exact for money and tax.
double: fast for science, tiny rounding errors.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

C# with AI in 3

dotnet new classlib + xunit.
decimal for money; check current rules.
dotnet test = proof.
Ravindra Bagale · ravindrabagale.com/ai-coding