简体   繁体   中英

Converting a Guid to Nullable Guid

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

new 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 (eg 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)

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM