当前位置: 移动技术网 > IT编程>开发语言>c# > C# 中GUID生成格式的四种方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
c#中guid的生成以及格式 1、guid是在system命名空间下的结构(struct)体,下面展示实例。 (1)创建一个guid帮助类(guidhelper)

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

参考:

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网