page contents

C# 中GUID生成格式的四种方法

本文讲述了C# 中GUID生成格式的四种方法!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-05-YC1AQW9C628aeba5587f2.png

本文讲述了C# 中GUID生成格式的四种方法!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

1、GUID是在System命名空间下的结构(struct)体,下面展示实例。
(1)创建一个GUID帮助类(GUIDHelper)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)使用实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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生成的四种格式

1
2
3
4
5
6
7
8
9
10
11
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}}

更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

如果你想用Python开辟副业赚钱,但不熟悉爬虫与反爬虫技术,没有接单途径,也缺乏兼职经验
关注下方微信公众号:Python编程学习圈,获取价值999元全套Python入门到进阶的学习资料以及教程,还有Python技术交流群一起交流学习哦。

attachments-2022-06-hUZS0Prv62b42622dd13d.jpeg

  • 发表于 2022-05-23 10:04
  • 阅读 ( 709 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1312 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章