• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

c# 搞代码 4年前 (2022-01-09) 24次浏览 已收录 0个评论

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.IsNu<a>本文来源gao($daima.com搞@代@#码8网^</a>llOrWhiteSpace(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}}

以上就是C# 中GUID生成格式有哪些方法的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C# 中GUID生成格式有哪些方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址