C# 中有哪些方法生成GUID

发布时间:2020-07-08 17:42:49 作者:Leah
来源:亿速云 阅读:427

C# 中有哪些方法生成GUID?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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-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}}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. C# 利用QRCode生成二维码图片
  2. c#开发ocx

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

csharp guid

上一篇:C#中关于Builder生成器模式解决配置电脑的问题

下一篇:静态类及其相关知识

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》