I am using Azure IoT Hub. In Azure portal, it is easy to see how many cloud to device messages are pending in the queue.
I want to read this value using C# SDK
I finally found the working answer as follows:
using Microsoft.Azure.Devices;
RegistryManager registryManager = RegistryManager.CreateFromConnectionString("registryreadconnectionstring");
Device device = await registryManager.GetDeviceAsync("device-id");
var count = device.CloudToDeviceMessageCount;