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
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
- .NET SDK 10: the current long-term support version. Check with
dotnet --version. - xUnit: a popular .NET test library. The
xunitproject 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
Install and set up
- Install the .NET 10 SDK from dotnet.microsoft.com (or with the Visual Studio Installer).
- Check:
dotnet --versionshows 10.x. - Create the projects:
dotnet new classlib -n Billing dotnet new xunit -n Billing.Tests dotnet add Billing.Tests reference Billing
Step 1 — the 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
Write xUnit tests for Gst.Add: 18% on 1000, rounding on 100.25 at 5%, a bad rate, and a negative price.
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: 4Settings and config
- Turn on nullable checks:
<Nullable>enable</Nullable>in the .csproj (new templates do this). It catches AI null mistakes. - Treat warnings as errors in CI:
dotnet build -warnaserror. - Put your rules (".NET 10, xUnit, decimal for money") in
.github/copilot-instructions.mdorCLAUDE.md.
Good C# prompts (reuse these)
- “Convert this class to a record with init-only properties.”
- “Make this method async and pass a CancellationToken.”
- “Add ILogger logging, but never log card numbers or passwords.”
- “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 forawait.
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
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.