The function pointer is used to store the reference of the method. The pointer is similar to delegate in C#, but it has some differences from the delegate.
Difference between function pointer and delegate:
| Function pointer | Delegate |
| 1.Function pointer should have return type except “void” | 1. Delegate can have any return type. |
| 2. It has capable to hold one function reference at a time. | 2. It can hold multiple method reference at time. |
| 3. The pointer method should has at least one argument | 3. It is not necessary to has any arguments. |
Syntax of Function Pointer:
- public delegate TResult Func<[in T,…], out TResult>(
- T arg
- )
Example:
- class Program
- {
- static Func<string,string> FunctionPTR = null;
- static Func<string,string, string> FunctionPTR1 = null;
- static string Display(string message)
- {
- Console.WriteLine(message);
- return null;
- }
- static string Display(string message1,string message2)
- {
- Console.WriteLine(message1);
- Console.WriteLine(message2);
- return null;
- }
- static void Main(string[] args)
- {
- FunctionPTR = Display;
- FunctionPTR1= Display;
- FunctionPTR("Welcome to function pointer sample.");
- FunctionPTR1("Welcome","This is function pointer sample");
- Console.ReadKey();
- }
- }


Bogdan MartPosted Apr 25, 2021, 12:50 AM EDT
This is not function pointers! System.Func are just predefined delegates that are lining in standard library and are used for functional programming in C#. They exist to allow different library authors use same types for delegates, to be compatible. Therea are also System.Action which is the same as Func but with void return. Actuall function pointers is nocept from C language that could land to C# one day https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/function-pointerss not function pointers!
Metta ChittibabuPosted Aug 12, 2018, 12:57 AM EDT
Fun delegate includes zero or more(max 16) input arguments and 1 out put argument.
Jaipal ReddyPosted Jun 21, 2015, 8:45 PM EDT
welcom
Santhakumar MunuswamyPosted Jun 18, 2015, 10:36 AM EDT
Welcome
Santhakumar MunuswamyPosted Jun 18, 2015, 10:36 AM EDT
Good Start