πŸš€ 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 Functions
  • Mediat β€” CQRS and MediatR flow in Azure Functions
  • Middleware β€” Azure Functions middleware examples for .NET 6 and .NET 8

🧠 Core .NET Concepts

  • SemaphoreSlim β€” throttling concurrent async work
  • TaskErrors β€” understanding exceptions in async workflows
  • UnsafeAs β€” unsafe conversions and pointer-based operations
  • JsonSearchPatterns β€” practical JSONPath traversal and filtering

πŸ—„οΈ Data & Integration

πŸ–₯️ UI & UX

  • FourKWinForm β€” Windows Forms app with DPI awareness
  • SpectreDemo β€” polished terminal demos using Spectre.Console

πŸ§ͺ Testing & Utilities

  • Fake β€” unit testing log output with FakeLogger<T>
  • PublicIP β€” quick public IP lookup sample

βš™οΈ 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:

  1. Mediat for architecture patterns
  2. Middleware for Azure Functions internals
  3. SQLWrites for database performance patterns
  4. SpectreDemo for terminal UX magic
  5. PythonBridge for cross-language integration

Happy coding! πŸ’»βœ¨


Read all C# and .NET Code Examples guides online Β· Project overview Β· All developer projects