Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Use Native AOT for High-Performance Apps

- 09.07.26 - ErcanOPAK

🚀 Native AOT = Ultra Performance

JIT compilation has overhead. Native AOT compiles ahead-of-time. Fast startup, low memory, native performance.

📝 AOT Setup

# Project file
<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <PublishAot>true</PublishAot>
        <TrimMode>full</TrimMode>
        <EnableAotAnalyzer>true</EnableAotAnalyzer>
    </PropertyGroup>
</Project>

# Publish with AOT
dotnet publish -c Release -r win-x64 --self-contained

# Or
dotnet publish -c Release -r linux-x64 --self-contained

# Or
dotnet publish -c Release -r osx-x64 --self-contained

# AOT-specific features
[UnconditionalSuppressMessage]
[DynamicallyAccessedMembers]

# AOT limitations
- No reflection (limited)
- No dynamic code generation
- No System.Reflection.Emit
- Limited dynamic assembly loading

🎯 AOT Optimizations

// Use source generators for JSON
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(User))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}

// Use with AOT
var user = JsonSerializer.Deserialize(json, SourceGenerationContext.Default.User);

// Avoid reflection in AOT
// Instead use source generators or compile-time code

// Performance benefits
- ~50% faster startup
- ~30% less memory
- ~20% smaller binary
- Native performance

// Use with Docker (smaller images)
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine
COPY /app/publish /app
WORKDIR /app
ENTRYPOINT ["/app/myapp"]

// AOT with minimal APIs
var builder = WebApplication.CreateSlimBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello AOT!");
app.Run();

// Trimming warnings
// Use [DynamicallyAccessedMembers] for reflection
// Use source generators instead of reflection

💡 AOT Best Practices

  • Use source generators for serialization
  • Avoid dynamic code generation
  • Test with AOT early in development
  • Use trim-friendly libraries
  • Profile AOT vs JIT performance

“Native AOT delivers ultra performance. Fast startup, low memory. Essential for high-performance .NET apps.”

— Performance Engineer

Related posts:

C#: Why LINQ's Deferred Execution Silently Re-Runs Your Database Query Every Time You Enumerate It

C#: Why LINQ's Deferred Execution Silently Re-Runs Your Database Query Every Time You Enumerate It

C#: Span<T> ile Memory Optimization

C#: Span<T> ile Memory Optimization

C#: Why Pattern Matching Against a Null Object Doesn't Throw, and Why That's Not Always Safe

C#: Why Pattern Matching Against a Null Object Doesn't Throw, and Why That's Not Always Safe

Views: 20

Post navigation

C#: Use Source Generators for Code Generation
Visual Studio: Use Code Snippets to Write Code Faster

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com