Search code examples
c#azure.net-coreazure-storage

Different usage of methods returning Azure.Response<T>


Both methods CreateBlobContainerAsync and GetPropertiesAsync return a Task<Response<T>>. Here's an example that uses them. It can easily be reproduced in a VS .NET 7 project that references package Azure.Storage.Blobs, version 12.14.1.

BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync("container" + Guid.NewGuid().ToString());
Response<BlobContainerProperties> properties = await containerClient.GetPropertiesAsync();

Azure.Resonse<T> defines an implicit operator. The first call in the example converts implicitly, and I would expect the same for the second call.

Why are the method usages different?


Solution

  • Implicit conversions allow you to convert a type without having to perform a cast. It does however require your code to use it as the implicit type. In your example you could do so by changing the type of the variable it's saved in e.g.

    BlobContainerProperties properties = await containerClient.GetPropertiesAsync();
    

    Another example would be a method requiring a parameter of a certain type. If your class allows an implicit cast you don't have to perform the cast yourself.