The sample uses the C# service SDK. The first important step is to connect to your IoT hub using a connection string you passed in from the command line:
using var serviceClient = ServiceClient.CreateFromConnectionString(parameters.HubConnectionString);
The sample then calls the InvokeMethodAsync method. This async method creates a CloudToDeviceMethod instance to call the SetTelemetryInterval method on your device. The device ID is also passed in as a parameter to the sample:
// Invoke the direct method on the device, passing the payload.
private static async Task InvokeMethodAsync(string deviceId, ServiceClient serviceClient)
{
var methodInvocation = new CloudToDeviceMethod("SetTelemetryInterval")
{
ResponseTimeout = TimeSpan.FromSeconds(30),
};
methodInvocation.SetPayloadJson("10");
Console.WriteLine($"Invoking direct method for device: {deviceId}");
// Invoke the direct method asynchronously and get the response from the simulated device.
CloudToDeviceMethodResult response = await serviceClient.InvokeDeviceMethodAsync(deviceId, methodInvocation);
Console.WriteLine($"Response status: {response.Status}, payload:\n\t{response.GetPayloadAsJson()}");
}
You can find a detailed walk through here: https://video2.skills-academy.com/azure/iot-hub/quickstart-control-device?pivots=programming-language-csharp