Create an ordered guid

Source: Internet
Author: User
Public static class API {// <summary> // create an ordered guid. /// </Summary> /// <returns> </returns> Public static guid newsequentialguid () {guid result; uuidcreatesequential (out result); return result ;} /// <summary> /// http://stackoverflow.com/questions/211498/is-there-a-net-equalent-to-sql-servers-newsequentialid /// </Summary> /// <Param name = "guid"> </param> /// <returns> </ returns> [dllimport ("rpcrt4.dll ", setlasterror = true)] Private Static extern I NT uuidcreatesequential (Out guid); [obsolete ("this is not ordered and the test failed. ")] // <Summary> // http://snipplr.com/view.php? Codeview & id = 29304 /// </Summary> /// <returns> </returns> Public static guid Newcomb () {byte [] datebytes = bitconverter. getbytes (datetime. now. ticks); byte [] guidbytes = guid. newguid (). tobytearray (); // copy the last six bytes from the date to the last six bytes of the guid array. copy (datebytes, datebytes. length-7, guidbytes, guidbytes. length-7, 6); return New GUID (guidbytes );}}

Test

    [TestFixture]    public class APITest    {        [Test]        public void testUuidCreateSequential()        {            List<Guid> list = new List<Guid>();            for (int i = 0; i < 100; i++)            {                Guid id = API.NewSequentialGuid();                list.Add(id);                Console.WriteLine(id);            }            for (int i = 0; i < list.Count - 1; i++)            {                Assert.Less(list[i], list[i + 1]);            }        }        [Test]        public void testNewComb()        {            List<Guid> list = new List<Guid>();            for (int i = 0; i < 100; i++)            {                Guid id = API.NewComb();                list.Add(id);                Console.WriteLine(id);            }            for (int i = 0; i < list.Count - 1; i++)            {                Assert.Less(list[i], list[i + 1]);            }        }    }

  

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.