英文:
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?
英文:
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?
答案1
得分: 0
隐式转换允许您在无需执行强制转换的情况下将类型进行转换。但是,它要求您的代码将其用作隐式类型。在您的示例中,您可以通过更改变量的类型来实现:
BlobContainerProperties properties = await containerClient.GetPropertiesAsync();
另一个示例是某个方法需要特定类型的参数。如果您的类允许隐式转换,您就无需自行执行强制转换。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论