Is this an idiomatic way to convert a Guid
to a Guid?
?
new Guid?(new Guid(myString));
A Guid is a struct , those can't be null.
The Parse method trims any leading or trailing white space from input and converts the string representation of a GUID to a Guid value. This method can convert strings in any of the five formats produced by the ToString(String) and ToString(String, IFormatProvider) methods, as shown in the following table.
You can use these methods to get an empty guid. The result will be a guid with all it's digits being 0's - " 00000000-0000-0000-0000-000000000000 ". In newer C# versions, default(Guid) and default are the same, too.
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)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With