C# 中GUID產生格式有哪些方法

來源:互聯網
上載者:User
1、GUID是在System命名空間下的結構(struct)體,下面展示執行個體。
(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)顯示結果

預設格式:4575c4b3-7997-4f11-acd9-f107258e9adc
N格式:a53a7186b583483aa4580519034e8095
D格式:5ae7f002-a989-4345-864b-3bcfbe09e1da
B格式:{d9762660-8461-4c44-b714-8ffad6e1b79c}
P格式:(694ce704-0a7d-41d5-a25a-4eaedf7db50d)
X格式:{0x75198f26,0xac4e,0x42c8,{0x96,0x88,0xcc,0x91,0xe0,0xa6,0x9b,0x21}

在C#中GUID產生的四種格式

var uuid = Guid.NewGuid().ToString(); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12var uuidN = Guid.NewGuid().ToString("N"); // e0a953c3ee6040eaa9fae2b667060e09 var uuidD = Guid.NewGuid().ToString("D"); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12var 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}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.