Q# – Function Objects

A delegate in C# is like a function object in C++. The delegate object can be passed to a code. Delegate encapsulates a reference to a method inside the object.  The referenced method is executed during runtime. A Test FunctionObject is shown below.

public delegate void TestFunctionObject();using System;

using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;

namespace Hello
{
    class Driver
    {
        public static void ParameterFunction()
        {
            Console.WriteLine("Invoked by Delegate");
        }
        static void Main(string[] args)
        {

            TestFunctionObject testFunction = new TestFunctionObject(ParameterFunction);
            testFunction();

        }

    }

Run the following commands  to execute the project in Visual Studio Code:

^F5 

In the terminal, you can execute the build by running the command below.

dotnet run


The screenshot of the output is attached below:

Leave a comment