tags:

views:

232

answers:

2

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

new Guid?(new Guid(myString));
+10  A: 

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)
Jon Skeet
Strange how Guid doesn't Parse or TryParse methods.
David Kemp
@David: Agreed.
Jon Skeet
@David: They're coming in .NET 4, better late than never! http://msdn.microsoft.com/en-us/library/system.guid_members%28VS.100%29.aspx
LukeH
A: 

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

Grzenio