本文整理汇总了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}}