3

I have string type value like "e2ddfa02610e48e983824b23ac955632". I need to add - in this code means convert in Guid.

EntityKey = "e2ddfa02610e48e983824b23ac955632";
Id = (Guid)paymentRecord.EntityKey;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Kapil Garg
  • 3,100
  • 5
  • 19
  • 19

3 Answers3

13

Just a simple creation:

  String source = "e2ddfa02610e48e983824b23ac955632";

  Guid result = new Guid(source);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
9

You could do :

Guid guid;
if (Guid.TryParse("e2ddfa02610e48e983824b23ac955632", out guid))
{
    // succeed...
}
else
{
    // failed...
}

Edit : Like @Silvermind said, if you know the format of the input, you can use Guid.TryParseExact with the "N" format in your case.

Morgan M.
  • 846
  • 2
  • 9
  • 27
2

For parsing the string to a Guid. You can do this:

var guid= "e2ddfa02610e48e983824b23ac955632";
var result= Guid.ParseExact(guid,"N")

Or if you prefer to have it in a try parse. You can also do this:

Guid result;
if(Guid.TryParseExact(guid,"N",out result))
{
    //Do something
}

The "N" is a format which indicates that the string will be format with 32 digits without "-"

Reference:

Arion
  • 31,011
  • 10
  • 70
  • 88