本文整理匯總了C#中System.Guid.ParseExact方法的典型用法代碼示例。如果您正苦於以下問題:C# Guid.ParseExact方法的具體用法?C# Guid.ParseExact怎麽用?C# Guid.ParseExact使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Guid
的用法示例。
在下文中一共展示了Guid.ParseExact方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: for
// Define an array of all format specifiers.
string[] formats = { "N", "D", "B", "P", "X" };
Guid guid = Guid.NewGuid();
// Create an array of valid Guid string representations.
var stringGuids = new string[formats.Length];
for (int ctr = 0; ctr < formats.Length; ctr++)
stringGuids[ctr] = guid.ToString(formats[ctr]);
// Parse the strings in the array using the "B" format specifier.
foreach (var stringGuid in stringGuids)
{
try
{
Guid newGuid = Guid.ParseExact(stringGuid, "B");
Console.WriteLine($"Successfully parsed {stringGuid}");
}
catch (ArgumentNullException)
{
Console.WriteLine("The string to be parsed is null.");
}
catch (FormatException)
{
Console.WriteLine($"Bad Format: {stringGuid}");
}
}
輸出:
Bad Format: eb5c8c7d187a44e68afb81e854c39457 Bad Format: eb5c8c7d-187a-44e6-8afb-81e854c39457 Successfully parsed {eb5c8c7d-187a-44e6-8afb-81e854c39457} Bad Format: (eb5c8c7d-187a-44e6-8afb-81e854c39457) Bad Format: {0xeb5c8c7d,0x187a,0x44e6,{0x8a,0xfb,0x81,0xe8,0x54,0xc3,0x94,0x57}}