将 Guid 转换为可为 Null 的 Guid

发布于 2024-08-19 21:56:57 字数 116 浏览 2 评论 0原文

这是将 Guid 转换为 Guid 的惯用方法吗?

new Guid?(new Guid(myString));

Is this an idiomatic way to convert a Guid to a Guid??

new Guid?(new Guid(myString));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韵柒 2024-08-26 21:56:57

不,这是:

Guid? foo = new Guid(myString);

TNullable 的隐式转换 - 您不需要做任何特殊的事情。或者,如果您不处于隐式转换起作用的情况(例如,您尝试调用一个同时具有可为空和不可为空类型重载的方法),则可以对其进行强制转换:

(Guid?) new Guid(myString)

No, this is:

Guid? foo = new Guid(myString);

There's an implicit conversion from T to Nullable<T> - you don't need to do anything special. Or if you're not in a situation where the implicit conversion will work (e.g. you're trying to call a method which has overloads for both the nullable and non-nullable types), you can cast it:

(Guid?) new Guid(myString)
紫﹏色ふ单纯 2024-08-26 21:56:57

只需转换它: (Guid?)(new Guid(myString))

还有一个隐式转换,所以这也可以正常工作:
指导? g = 新 Guid(myString);

just cast it: (Guid?)(new Guid(myString))

there is also an implicit cast, so this would work fine as well:
Guid? g = new Guid(myString);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文