C〓〓の中でGUIDはフォーマットの4種類の方法を生成します。


C〓〓の中でGUIDの生成とフォーマット
1、GUIDはSystem名前空間における構造体であり、以下に実例を示す。
(1)GUIDヘルプクラスを作成する(GUIDHelper)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebDemo.guid
{
  public class GuIdHelper
  {
    /// <summary>
    /// GUID  
    /// </summary>
    /// <param name="format">      N、D、B、P、X</param>
    /// <returns></returns>
    public static string GetNewGuId(string format="")
    {
      if (string.IsNullOrWhiteSpace(format))
        return Guid.NewGuid().ToString();
      else
        return Guid.NewGuid().ToString(format);
    }
  }
}
(2)使用例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace WebDemo.guid
{
  public partial class Index : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

      StringBuilder str = new StringBuilder();
      string[] array = {"","N","D","B","P","X" };
      foreach (var item in array)
      {
        if (string.IsNullOrWhiteSpace(item))
          str.AppendFormat("    :{0}", GuIdHelper.GetNewGuId());
        else
          str.AppendFormat("<br />{0}  :{1}", item, GuIdHelper.GetNewGuId(item));
      }
      Response.Write(str.ToString());
    }
  }
}
(3)結果を表示する
デフォルトのフォーマット:4575 c 4 b 3-7997-4 f 11-acd 9-f 107258 e 9 adc
Nフォーマット:a 53 a 7186 b 583483 a 4580519034 e 8095
Dフォーマット:5 ae 7 f 002-a 989-4345-86 b-3 bcf be 09 e 1 da
Bフォーマット:{d 9762660-8461-4 c 44-b 714-8 ffad 6 e 1 b 79 c}
Pフォーマット:(694 CE 704-0 a 7 d-41 d 5-a 25 a-4 eaedf 7 db 50 d)
Xフォーマット:{0 x 75198 f 26,0 xac 4 e、0 x 42 c 8、{0 x 96,0 x 88,0 xcc、0 x 91,0 xe 0,0 xa 6,0 x 9 b、0 x 21}
C〓〓の中でGUIDの生成の4種類のフォーマット

var uuid = Guid.NewGuid().ToString(); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12 
 
var uuidN = Guid.NewGuid().ToString("N"); // e0a953c3ee6040eaa9fae2b667060e09  
 
var uuidD = Guid.NewGuid().ToString("D"); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12 
 
var uuidB = Guid.NewGuid().ToString("B"); // {734fd453-a4f8-4c5d-9c98-3fe2d7079760} 
 
var uuidP = Guid.NewGuid().ToString("P"); // (ade24d16-db0f-40af-8794-1e08e2040df3) 
 
var uuidX = Guid.NewGuid().ToString("X"); // {0x3fa412e3,0x8356,0x428f,{0xaa,0x34,0xb7,0x40,0xda,0xaf,0x45,0x6f}} 
参考:https://msdn.microsoft.com/en-us/library/97af8hh4.aspx