Skip to content

Instantly share code, notes, and snippets.

@dresser
Last active October 22, 2015 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dresser/23c6246ccd7525366856 to your computer and use it in GitHub Desktop.
Save dresser/23c6246ccd7525366856 to your computer and use it in GitHub Desktop.
void Main()
{
var guid = new Guid();
guid.ToString("N").Dump(); //Returns "00000000000000000000000000000000"
guid.ToString("D").Dump(); //Returns "00000000-0000-0000-0000-000000000000"
guid.ToString("P").Dump(); //Returns "(00000000-0000-0000-0000-000000000000)"
guid.ToString("B").Dump(); //Returns "{00000000-0000-0000-0000-000000000000}"
guid.ToString("X").Dump(); //Returns "{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}"
//ID.Parse
ID.Parse("00000000000000000000000000000000").Dump(); //OK
ID.Parse("00000000-0000-0000-0000-000000000000").Dump(); //OK
ID.Parse("(00000000-0000-0000-0000-000000000000)").Dump(); //OK
ID.Parse("{00000000-0000-0000-0000-000000000000}").Dump(); //OK
ID.Parse("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}").Dump(); //OK
ID.Parse("").Dump(); //Throws FormatException
ID.Parse(" ").Dump(); //Throws FormatException
ID.Parse("bob").Dump(); //Throws FormatException
//ID.TryParse
ID value;
ID.TryParse("00000000000000000000000000000000", out value).Dump(); // false
ID.TryParse("00000000-0000-0000-0000-000000000000", out value).Dump(); // true
ID.TryParse("(00000000-0000-0000-0000-000000000000)", out value).Dump(); // false
ID.TryParse("{00000000-0000-0000-0000-000000000000}", out value).Dump(); // true
ID.TryParse("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}", out value).Dump(); // false
//ID.IsID
ID.IsID("00000000000000000000000000000000").Dump(); // false
ID.IsID("00000000-0000-0000-0000-000000000000").Dump(); // true
ID.IsID("(00000000-0000-0000-0000-000000000000)").Dump(); // false
ID.IsID("{00000000-0000-0000-0000-000000000000}").Dump(); // true
ID.IsID("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}").Dump(); // false
//ID.Encode
var id = new ID();
ID.Encode(id).Dump();
//ID.Decode
ID.Decode("00000000000000000000000000000000").Dump(); // returns ID
ID.Decode("00000000-0000-0000-0000-000000000000").Dump(); // returns ID
ID.Decode("(00000000-0000-0000-0000-000000000000)").Dump(); // Throws FormatException
ID.Decode("{00000000-0000-0000-0000-000000000000}").Dump(); // returns ID
ID.Decode("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}").Dump(); // throws FormatException
//ID.ParseArray
ID.ParseArray("00000000000000000000000000000000|00000000000000000000000000000000").Dump(); // returns empty array
ID.ParseArray("00000000-0000-0000-0000-000000000000|00000000-0000-0000-0000-000000000000").Dump(); // returns array of two elements
ID.ParseArray("(00000000-0000-0000-0000-000000000000)|(00000000-0000-0000-0000-000000000000)").Dump(); // returns empty array
ID.ParseArray("{00000000-0000-0000-0000-000000000000}|{00000000-0000-0000-0000-000000000000}").Dump(); // returns array of two elements
ID.ParseArray("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}|{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}").Dump(); // returns empty array
ID.ParseArray("|{00000000-0000-0000-0000-000000000000}||{00000000-0000-0000-0000-000000000000}|").Dump(); // returns array of two elements
ID.ParseArray("non|sense").Dump(); // returns empty array
ID.ParseArray((string)null, true).Dump(); // returns null
ID.ParseArray((string)null, false).Dump(); // returns empty array
//ID.ArrayToString
var ids = new ID[] { new ID(), new ID() };
ID.ArrayToString(ids).Dump(); //returns "{00000000-0000-0000-0000-000000000000}|{00000000-0000-0000-0000-000000000000}" (actual GUID values will differ!)
ID.ArrayToString(ids, ',').Dump(); //returns "{00000000-0000-0000-0000-000000000000},{00000000-0000-0000-0000-000000000000}" (actual GUID values will differ!)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment