π C# and .NET Code Examples β Azure Functions, Async, SQL Server, MediatR and More β¨
Small, runnable C#/.NET projects that each demonstrate one practical pattern: Azure Functions, dependency injection, async concurrency, SQL Server writes, Windows Forms, terminal UI, and AI integration.
Overview
This repository is a hands-on collection of C# code examples for .NET 6, .NET 8, and .NET 10. Each project is intentionally small and focused on one concept, so you can read the code, run it, and reuse the pattern in your own application.
Topics include Azure Functions isolated worker middleware, Durable Functions orchestration, keyed dependency injection, MediatR / CQRS, SemaphoreSlim throttling, Task.WhenAll exception handling, SQL Server bulk copy and table-valued parameter upserts, FakeLogger unit testing, JSONPath queries, Unsafe.As, Spectre.Console, Python.NET interop, and Azure OpenAI chat completion.
Use the project table to choose an example, then follow that folderβs README for setup, configuration and known limitations.
Project Highlights
| Project | Focus | What it demonstrates | Target |
|---|---|---|---|
| Durable | Azure Durable Functions | Stateful, chained orchestration pipelines in the isolated worker model | .NET 8 |
| Fake | Logging + unit testing | Testing ILogger code with FakeLogger<T> from Microsoft.Extensions.Diagnostics.Testing and MSTest |
.NET 8 |
| FourKWinForm | Windows Forms | High-DPI (SystemAware) desktop form |
.NET 8 |
| JsonSearchPatterns | JSON processing | Nine JSONPath query patterns with Newtonsoft.Json SelectTokens |
.NET 6 |
| KeyedService | Dependency injection | Keyed scoped services (AddKeyedScoped, [FromKeyedServices]) in Azure Functions |
.NET 8 |
| Mediat | CQRS + MediatR | Multi-layer Azure Functions solution with commands, queries, and pipeline behaviours | .NET 8 |
| Middleware | Azure Functions | IFunctionsWorkerMiddleware request transformation and exception handling |
.NET 6 / .NET 8 |
| PublicIP | Networking | Fetching the public IP address from the ipify API with HttpClient |
.NET 6 |
| PythonBridge | Interop | Calling Python from C# with Python.NET (pythonnet) |
.NET 8 |
| SemaphoreSlim | Concurrency | Limiting concurrent async downloads with SemaphoreSlim |
.NET 8 |
| Simple_ChatCompletion | AI integration | Azure OpenAI chat completion via the Azure.AI.OpenAI package and via raw REST |
.NET 8 |
| SpectreDemo | Terminal UI | Live panels, multi-task progress bars, and prompts with Spectre.Console | .NET 8 |
| SQLWrites | SQL Server | Three upsert strategies β per-row transactions, table-valued parameters, and bulk copy β with retry and optimistic concurrency | .NET 10 |
| TaskErrors | Async error handling | Capturing all exceptions from Task.WhenAll, not just the first |
.NET 8 |
| UnsafeAs | Low-level .NET | Zero-copy reinterpretation with Unsafe.As<TFrom, TTo> and unsafe pointers |
.NET 8 |
Every project folder has its own readme with detailed explanations, configuration, and sample requests.
π§ Repository Map
βοΈ Azure Functions & Cloud Patterns
Durableβ Durable Functions orchestration and function chaining (isolated worker, Functions v4)KeyedServiceβ keyed dependency injection with Azure FunctionsMediatβ CQRS and MediatR flow in Azure FunctionsMiddlewareβ Azure Functions middleware examples for .NET 6 and .NET 8
π§ Core .NET Concepts
SemaphoreSlimβ throttling concurrent async workTaskErrorsβ understanding exceptions in async workflowsUnsafeAsβ unsafe conversions and pointer-based operationsJsonSearchPatternsβ practical JSONPath traversal and filtering
ποΈ Data & Integration
SQLWritesβ SQL Server upsert strategies compared (normal, TVP, bulk)PythonBridgeβ bridging C# with PythonSimple_ChatCompletionβ Azure OpenAI chat completion examples
π₯οΈ UI & UX
FourKWinFormβ Windows Forms app with DPI awarenessSpectreDemoβ polished terminal demos using Spectre.Console
π§ͺ Testing & Utilities
βοΈ Tech Stack
- C# / .NET 6, .NET 8, .NET 10
- Azure Functions v4 (isolated worker) and Durable Functions
- SQL Server (
Microsoft.Data.SqlClient) - MediatR
- Python.NET (
pythonnet) - Azure OpenAI (
Azure.AI.OpenAI) - Spectre.Console
- Newtonsoft.Json
- MSTest and
Microsoft.Extensions.Diagnostics.Testing - Windows Forms
π Quick Start
Each project is self-contained. Install the .NET SDK version that the project targets (see the table above), pick one folder, and run it from there:
git clone https://github.com/gcfernando/csharp_codes.git
cd csharp_codes
# Example: run a single console project
cd SpectreDemo
dotnet run
Every project includes a Visual Studio solution (.sln) file. Folders that contain several projects (for example SQLWrites, Mediat, and Fake) need you to choose which project to run β open the solution in Visual Studio, or pass the project path from the repository root, for example:
dotnet run --project SQLWrites/UseNormal
The Azure Functions samples (Durable, KeyedService, Mediat, Middleware) require Azure Functions Core Tools and are started from the function project folder with:
func start
Some samples need external resources β for example a SQL Server database (SQLWrites), an Azure OpenAI endpoint and key (Simple_ChatCompletion), or a local Python installation (PythonBridge). The project readme lists what is required.
β FAQ
What is this repository for?
It is a reference of focused C#/.NET examples. Each folder answers one practical question, such as βhow do I add middleware to an Azure Functions isolated worker app?β or βhow do I catch every exception from Task.WhenAll?β.
Which .NET version do I need?
It depends on the project: most target .NET 8, JsonSearchPatterns, PublicIP, and the Middleware/Dotnet6 samples target .NET 6, and SQLWrites targets .NET 10. FourKWinForm requires Windows.
Are these production-ready libraries?
No. They are learning-oriented demos. For production-grade packages, see Nilog (zero-allocation ILogger extensions) and Glass.Message (a modern WinForms MessageBox replacement).
Where can I find similar examples in Python?
See the companion Python projects collection β its LoadLibraries project shows the reverse direction of PythonBridge: calling a .NET library from Python.
π‘ Notes
- Most projects are learning-oriented and intentionally compact.
- This repo focuses on experiments, demos, and practical patterns rather than production-ready apps.
- Several projects can be opened directly in Visual Studio or run via
dotnet run.
π Summary
This is a compact, colorful collection of .NET experiments covering cloud-native patterns, async techniques, AI integration, UI work, database writes, and system-level programming. It is a great sandbox for learning by building small, focused examples.
If you want to explore the repo in a more hands-on way, start with:
Mediatfor architecture patternsMiddlewarefor Azure Functions internalsSQLWritesfor database performance patternsSpectreDemofor terminal UX magicPythonBridgefor cross-language integration
π Related Projects
- Python projects β standalone Python scripts and apps
- VB.NET Windows Forms examples β Visual Basic desktop experiments
- Developer knowledge base β includes a complete C#/.NET OOP guide and an Azure cheat sheet
Happy coding! π»β¨
Documentation links
Read all C# and .NET Code Examples guides online Β· Project overview Β· All developer projects