C# 中GUID生成格式的四种方法
发布时间:2020-12-15 07:39:25 所属栏目:百科 来源:网络整理
导读: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 clas
C#中GUID的生成以及格式 1、GUID是在System命名空间下的结构(struct)体,下面展示实例。 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 在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}} 参考:https://msdn.microsoft.com/en-us/library/97af8hh4.aspx (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- postgresql – 如何在pgsql中动态创建触发器函数?
- 用React+Redux+ES6写一个最傻瓜的Hello World
- Flash:flash 读取本地文件
- xml – 和模式定义之间的区别?
- git pull文件冲突解决办法
- Medical Image Segmentation Using New Hybrid Level-Set M
- react-native – 如何在React Native中打开现有项目?
- c# – .net Windows应用程序中的ObjectCache
- postgreSQL 的 lightweight lock
- ruby-on-rails – 将Ruby哈希转换为数组