Skip to content

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 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:

How to run code if not in Debug Mode in C#

The Hidden Cost of record Types in Hot Paths

C#: Clean Your Files with Global Usings and File-Scoped Namespaces

Post Views: 4

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 *

June 2026
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  
« May    

Most Viewed Posts

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

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files
  • Ajax: Add Custom Headers to Fetch Requests
  • JavaScript: Use console.table to Display Arrays as Tables
  • HTML: Use Spellcheck Attribute to Enable Browser Spell Check
  • CSS: Use user-select to Prevent Text Selection
  • Windows 11: Use Snipping Tool for Instant Screenshots

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (953)
  • How to add default value for Entity Framework migrations for DateTime and Bool (882)
  • Get the First and Last Word from a String or Sentence in SQL (838)
  • How to select distinct rows in a datatable in C# (808)
  • How to make theater mode the default for Youtube (805)

Recent Posts

  • C#: Use String Interpolation Instead of Concatenation
  • C#: Use Tuples to Return Multiple Values from Methods
  • SQL: Use ISNULL and NULLIF for Smart NULL Handling
  • .NET Core: Use Data Annotations for Model Validation
  • Git: Use Git Clean to Remove Untracked Files

Social

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