方法返回 Azure.Response<T> 的不同用法

huangapple go评论71阅读模式
英文:

Different usage of methods returning Azure.Response<T>

问题

Both methods CreateBlobContainerAsync and GetPropertiesAsync return a Task<Response&lt;T&gt;>. 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(&quot;container&quot; + Guid.NewGuid().ToString());
Response&lt;BlobContainerProperties&gt; properties = await containerClient.GetPropertiesAsync();

Azure.Resonse&lt;T&gt; 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&lt;T&gt;>. 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(&quot;container&quot; + Guid.NewGuid().ToString());
Response&lt;BlobContainerProperties&gt; properties = await containerClient.GetPropertiesAsync();

Azure.Resonse&lt;T&gt; 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.

huangapple
  • 本文由 发表于 2023年2月6日 16:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定