What is the difference between these two ways of converting a string to System.Guid
? Is there a reason to choose one over the other?
var myguid = Guid.Parse("9546482E-887A-4CAB-A403-AD9C326FFDA5");
or
var myguid = new Guid("9546482E-887A-4CAB-A403-AD9C326FFDA5");
Parses a span of characters into a value. Parse(ReadOnlySpan<Char>) Converts a read-only character span that represents a GUID to the equivalent Guid structure. Parse(String) Converts the string representation of a GUID to the equivalent Guid structure.
Description. The New-Guid cmdlet creates a random globally unique identifier (GUID). If you need a unique ID in a script, you can create a GUID, as needed.
To Generate a GUID in Windows 10 with PowerShell, Type or copy-paste the following command: [guid]::NewGuid() . This will produce a new GUID in the output. Alternatively, you can run the command '{'+[guid]::NewGuid(). ToString()+'}' to get a new GUID in the traditional Registry format.
A span containing the characters representing the GUID to convert. When this method returns, contains the parsed value. If the method returns true , result contains a valid Guid. If the method returns false , result equals Empty.
A quick look in the Reflector reveals that both are pretty much equivalent.
public Guid(string g) { if (g == null) { throw new ArgumentNullException("g"); } this = Empty; GuidResult result = new GuidResult(); result.Init(GuidParseThrowStyle.All); if (!TryParseGuid(g, GuidStyles.Any, ref result)) { throw result.GetGuidParseException(); } this = result.parsedGuid; } public static Guid Parse(string input) { if (input == null) { throw new ArgumentNullException("input"); } GuidResult result = new GuidResult(); result.Init(GuidParseThrowStyle.AllButOverflow); if (!TryParseGuid(input, GuidStyles.Any, ref result)) { throw result.GetGuidParseException(); } return result.parsedGuid; }
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