将 Guid 转换为可为 Null 的 Guid
这是将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这是:
从
T
到Nullable
的隐式转换 - 您不需要做任何特殊的事情。或者,如果您不处于隐式转换起作用的情况(例如,您尝试调用一个同时具有可为空和不可为空类型重载的方法),则可以对其进行强制转换:No, this is:
There's an implicit conversion from
T
toNullable<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))
还有一个隐式转换,所以这也可以正常工作:
指导? 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);