Skip to content
Volume C · The TensorCore.si Library

C#

Modern, productive and runs everywhere with .NET.

C# is Microsoft's flagship language for .NET. It powers web back ends, Windows and cross-platform apps, Unity games and Azure services, and Microsoft's Semantic Kernel brings LLM agents to it.

Prologue

Why learn C#?

C# combines strong typing with modern features like async/await, LINQ and records, plus excellent tooling in Visual Studio and VS Code.

  • Versatile: web, desktop, mobile, games and cloud with one language.
  • Unity: the most popular engine for indie and mobile games.
  • AI: Semantic Kernel, ML.NET and ONNX Runtime integrate AI into .NET apps.
Prologue

Set up and run your first program

Install the .NET 8 SDK or newer, then create a console app.

terminal
dotnet --version
dotnet new console -o HelloAi
cd HelloAi && dotnet run
Program.cs
var name = "Ada";
Console.WriteLine($"Hello, AI! Welcome to C#, {name}.");
TipTop-level statements mean Program.cs needs no class or Main method.
Chapter 1

Variables and types

Use var for inferred types or name the type. Strings support interpolation with $"...{x}". Nullable types end in ?.

Variables.cs
int layers = 32;
double lr = 3e-4;
var model = "llama3.1";
long parameters = 8_030_000_000;
string? note = null;            // may be null
Console.WriteLine($"{model}: {layers} layers, FP16 {parameters * 2 / 1e9:F1} GB, note={note ?? "none"}");
Chapter 2

Control flow and pattern matching

Loops include for, foreach and while. switch expressions and patterns make branching concise.

Control.cs
foreach (var gpu in new[] { "H100", "B200" }) Console.WriteLine(gpu);
int bits = 8;
string name = bits switch {
    32 => "FP32",
    16 => "FP16",
    8 => "FP8",
    _ => "other"
};
Console.WriteLine(name);
Chapter 3

Classes and records

Classes hold state and behaviour. record types are immutable data with value equality, ideal for messages and DTOs.

Records.cs
record Gpu(string Name, int VramGb)
{
    public bool Fits(double modelGb) => modelGb <= VramGb;
}
var g = new Gpu("RTX 5090", 32);
Console.WriteLine($"{g} fits 16 GB: {g.Fits(16)}");
Chapter 4

Collections and LINQ

List<T> and Dictionary<K,V> are the main collections. LINQ queries filter, sort and project data in one readable chain.

Linq.cs
var vram = new Dictionary<string, int> { ["H100"] = 80, ["H200"] = 141, ["B200"] = 180 };
var big = vram.Where(kv => kv.Value > 100).OrderBy(kv => kv.Key).Select(kv => kv.Key);
Console.WriteLine(string.Join(", ", big)); // B200, H200
Chapter 5

Async and await

I/O-bound work like HTTP calls uses async/await so threads are not blocked. HttpClient is the standard HTTP client.

Async.cs
using var http = new HttpClient();
try {
    string json = await http.GetStringAsync("http://localhost:11434/api/tags");
    Console.WriteLine(json[..Math.Min(120, json.Length)]);
} catch (HttpRequestException e) {
    Console.WriteLine($"Is Ollama running? {e.Message}");
}
Part II · AI with C#

AI with C#

C# is a strong choice for adding AI to business apps: chat features, document Q&A and agents inside .NET services. ML.NET trains classic models, and ONNX Runtime runs deep learning models fast on CPU or GPU.

The essential libraries

Semantic KernelMicrosoft's SDK for LLM plugins, planners and agents.
Microsoft.Extensions.AICommon abstractions for chat clients and embeddings.
ML.NETTrain and run classic ML models in .NET.
ONNX RuntimeFast inference for exported models.

Gradient descent from scratch

Learn y = 3x + 0.5 with plain loops.

Gradient.cs
var rng = new Random(0);
int n = 100;
var xs = Enumerable.Range(0, n).Select(i => -1 + 2.0 * i / (n - 1)).ToArray();
var ys = xs.Select(x => 3 * x + 0.5 + 0.1 * (rng.NextDouble() - 0.5)).ToArray();
double w = 0, b = 0, lr = 0.1;
for (int step = 0; step < 500; step++) {
    double gw = 0, gb = 0;
    for (int i = 0; i < n; i++) {
        double err = w * xs[i] + b - ys[i];
        gw += 2 * err * xs[i] / n; gb += 2 * err / n;
    }
    w -= lr * gw; b -= lr * gb;
}
Console.WriteLine($"w={w:F2} b={b:F2}");
Part III · Build an AI agent

Build an AI agent in C#

With System.Text.Json.Nodes you can run the agent loop directly against any OpenAI-compatible endpoint, here a local model in Ollama.

Your goal→Model plans→Tool call→Your code runs the tool→Result back to model→Final answer
Agent.cs
using System.Net.Http.Json;
using System.Text.Json.Nodes;

var http = new HttpClient();
var tools = JsonNode.Parse("""
[{"type":"function","function":{"name":"get_weather","description":"Weather for a city",
  "parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}}]
""");
var messages = new JsonArray(new JsonObject { ["role"] = "user", ["content"] = "Should I take an umbrella in Lahore today?" });

for (int round = 0; round < 5; round++) {
    var body = new JsonObject { ["model"] = "llama3.1", ["messages"] = messages.DeepClone(), ["tools"] = tools!.DeepClone() };
    var res = await http.PostAsJsonAsync("http://localhost:11434/v1/chat/completions", body);
    var msg = (await res.Content.ReadFromJsonAsync<JsonNode>())!["choices"]![0]!["message"]!;
    messages.Add(msg.DeepClone());
    if (msg["tool_calls"] is not JsonArray calls || calls.Count == 0) { Console.WriteLine(msg["content"]); break; }
    foreach (var call in calls) {
        var city = JsonNode.Parse(call!["function"]!["arguments"]!.GetValue<string>())!["city"]!.GetValue<string>();
        messages.Add(new JsonObject { ["role"] = "tool", ["tool_call_id"] = call["id"]!.GetValue<string>(), ["content"] = $"It is 24°C and sunny in {city}." });
    }
}

Semantic Kernel can do the same with C# methods marked as kernel functions, handling the loop for you.

Run it locallyInstall Ollama, run ollama pull llama3.1, and keep it running. The code talks to its OpenAI-compatible API at http://localhost:11434/v1, so the same program also works with vLLM, llama.cpp server or a cloud API by changing the URL, model name and adding an API key.
Part IV · Your path

C# learning roadmap

Tick items off as you go. Your progress is saved in this browser.

Beginner

Intermediate

Advanced

Part IV · Your path

Projects to build

Minimal APIA REST API with ASP.NET Core and SQLite.
Support botA chat assistant that answers from your docs.
Unity AI NPCA game character that talks through an LLM.
Reference

C# cheat sheet

TaskSyntax
PrintConsole.WriteLine(x);
Interpolate$"{name} has {n}"
Listvar l = new List<int>();
LINQl.Where(x => x > 0)
Recordrecord P(int X, int Y);
Asyncawait http.GetStringAsync(url)
Reference

Common mistakes

  • Creating a new HttpClient per request exhausts sockets; reuse one or use IHttpClientFactory.
  • async void swallows exceptions; return Task instead.
  • Ignoring nullable warnings leads to NullReferenceException.
Reference

Where to go next

On TensorCore.si: Java volume, How AI works.

Keyboard: [ previous language · ] next language · / search the library