In C#, variadic templates can be classes with repeated methods or use collection initializers and compile them into series of calls. Q# and C# are used to write quantum programs. Variadic templates help writing C# Drivers.
The code snippet shows Add Call with its own generic parameters and type inference.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;
namespace Hello
{
class Driver
{
static void Main(string[] args)
{
Container listContainer = new Container() {
() => new List<byte>()
};
Container memoryContainer = new Container() {
() => new MemoryStream()
};
Container credentialContainer = new Container() {
() => new CredentialCache()
};
Console.WriteLine(listContainer + "Type " + listContainer.GetType());
Console.WriteLine(memoryContainer + "Type " + memoryContainer.GetType());
Console.WriteLine(credentialContainer + "Type " + credentialContainer.GetType());
}
}
class Container : System.Collections.IEnumerable
{
public void Add<T>(Func<T> func)
{
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotSupportedException();
}
}
}
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:
