Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

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

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Use Function Pointers for High-Performance Callbacks

- 06.06.26 - ErcanOPAK

⚡ Delegate Overhead? Use Function Pointers.

Delegates are fast, but not zero-cost. Function pointers (C# 9+) call native code or high-frequency callbacks with minimal overhead.

📝 Basic Function Pointer

unsafe
{
    // Delegate (managed)
    Func addDelegate = (a, b) => a + b;
    
    // Function pointer (unmanaged)
    delegate* addPointer = &Add;
    
    int result1 = addDelegate(5, 3);  // Delegate invoke
    int result2 = addPointer(5, 3);   // Function pointer call (faster)
}

static int Add(int a, int b) => a + b;

🎯 Real-World Use

public unsafe class FastSort
{
    private delegate* _comparer;
    
    public FastSort(delegate* comparer)
    {
        _comparer = comparer;
    }
    
    public void Sort(int[] array)
    {
        // Fast comparison without delegate overhead
        Array.Sort(array, (a, b) => _comparer(a, b));
    }
}

// Usage
static int CompareInts(int a, int b) => a.CompareTo(b);

unsafe
{
    var sorter = new FastSort(&CompareInts);
    sorter.Sort(new[] { 5, 2, 8, 1, 9 });
}

💡 When to Use

  • P/Invoke to native C/C++ code
  • High-frequency callbacks (100k+ calls/second)
  • Game engines, real-time systems
  • When delegate allocation matters
  • Requires unsafe context (compiler flag)

“Sorting 50M items. Delegate comparer was bottleneck. Function pointer made it 20% faster. For high-performance code, every instruction matters.”

— High-Performance Computing Engineer

Related posts:

C#: Fix an IAsyncDisposable Never Awaited Inside a Using Block

C#: Fix an IAsyncDisposable Never Awaited Inside a Using Block

LINQ ToList() — The Performance Trap

LINQ ToList() — The Performance Trap

C#: Reducing Visual Noise with Target-typed 'new' Expressions

C#: Reducing Visual Noise with Target-typed 'new' Expressions

Views: 16
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

SQL: Use Full-Text Search for Fast Text Search
C#: Use ArrayPool to Reduce GC Pressure

Leave a Reply Cancel reply

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

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

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

Social

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