Sample code sharing for four methods of GUID generation format in C #

Source: Internet
Author: User
This article mainly describes the C # GUID generation format of four methods, the need for friends can refer to the following

The generation and format of GUIDs in C #

1, the GUID is the structure (struct) body under the System namespace, the example is shown below.
(1) Create a GUID helper Class (Guidhelper)

Using system;using system.collections.generic;using system.linq;using system.web;namespace WebDemo.guid{public  Class Guidhelper {//<summary>//GUID generated//</summary>//    <param name= " Format > can fill in 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) Examples of use

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 ("default format: {0}", Guidhelper.getnewguid ());        Else          str. AppendFormat ("<br/>{0} format: {1}", Item, GUIDHELPER.GETNEWGUID (item));      }      Response.Write (str. ToString ());}}}  

(3) Show results

Default format: 4575C4B3-7997-4F11-ACD9-F107258E9ADCN format: a53a7186b583483aa4580519034e8095d format: 5ae7f002-a989-4345-864b-3bcfbe09e1dab format: {d9762660-8461-4c44-b714-8ffad6e1b79c} P Format: (694ce704-0a7d-41d5-a25a-4eaedf7db50d) x format: {0x75198f26,0xac4e,0x42c8,{0x96,0x88,0xcc,0x91,0xe0,0xa6,0x9b, 0x21

Four formats that are generated by GUIDs in C #

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.