C#中GUID生成格式的四种方法的示例代码分享


本文摘自PHP中文网,作者黄舟,侵删。

这篇文章主要介绍了C# 中GUID生成格式的四种方法,需要的朋友可以参考下

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)显示结果

1

2

3

4

5

6

默认格式: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}}

以上就是C#中GUID生成格式的四种方法的示例代码分享的详细内容!

相关阅读 >>

c#中载入界面的经典实例

c#学习日记04---数据类型 之 整数类型

详解c#winform程序自动更新实现方法(图)

c#ref关键字的示例代码分享

c#实现检索不区分大小写并高亮显示的示例代码分享

c# 2.0 specification (四)

c#开发实例-订制屏幕截图工具(十)在截图中包含鼠标指针形状

c#在pdf中创建和填充域的详细介绍(图文)

c#中把image无损转换为icon的实例详解

关于c#中三个关键字params,ref,out的详细介绍

更多相关阅读请进入《GUID》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...