ExtensionMethod.NET Home of 876 C#, Visual Basic, F# and Javascript extension methods

String to Guid

This is used to be common in web api's. When you must to receive the guid as a string in parameter.

Source

public static Guid ToGuid(this string value)
{
    Guid.TryParse(value, out Guid result);
    return result;
}

Example

string value = "cf3d75d2-59b6-4d22-b74a-daa9d3dadd64";
value.ToGuid();

Console.WriteLine(value); // output: cf3d75d2-59b6-4d22-b74a-daa9d3dadd64

Author: meiro

Submitted on: 24 jul. 2023

Language: csharp

Type: System.Guid

Views: 1243