What are the methods in C # for GUID generation format

Source: Internet
Author: User
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" > format can be filled 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-F107258E9ADC
N Format: a53a7186b583483aa4580519034e8095
D Format: 5ae7f002-a989-4345-864b-3bcfbe09e1da
B 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-9fd484c2af12var uuidn = Guid.NewGuid (). ToString ("N"); e0a953c3ee6040eaa9fae2b667060e09 var Uuidd = Guid.NewGuid (). ToString ("D"); 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12var 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}}

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.