How to generate the short unique id using C#
2011-06-13 14:50
525 查看
It’s very common to create unique id in our application, like as order identifier,user identifier.etc. There are also many ways to generate the unique id in C#. The simplest approach is generating GUID by GUID struct built in .net framework. like as:
But, the length of id that created by this approach is 36 digits. It’s long, we want to make short version of unique id instead of long version of unique id.
It’s hard to create global unique id less than 36 digits. In reality, we also do not need the global unique id. sometimes, unique in machine level just enough for us.There is a easy way to generate unique id that do not global unique and with a slight performance overhead.
This approach utilize DateTime.Ticks property to generating unique id.The Ticks property for DateTime represents how number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001. The step of generating unique id are:
hold on 1 minisecond for unique.
get how 100-nanoseconds have elapsed since specific time point.
convert the value of time ticks from decimal to alphabet.
Here is source code of unique id generator.
Enjoy !
string id = Guid.NewGuid().ToString().ToLower()
But, the length of id that created by this approach is 36 digits. It’s long, we want to make short version of unique id instead of long version of unique id.
It’s hard to create global unique id less than 36 digits. In reality, we also do not need the global unique id. sometimes, unique in machine level just enough for us.There is a easy way to generate unique id that do not global unique and with a slight performance overhead.
This approach utilize DateTime.Ticks property to generating unique id.The Ticks property for DateTime represents how number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001. The step of generating unique id are:
hold on 1 minisecond for unique.
get how 100-nanoseconds have elapsed since specific time point.
convert the value of time ticks from decimal to alphabet.
Here is source code of unique id generator.
public class IdGenerator { private static object instance = new object(); public static string NewId() { lock (instance) { Thread.Sleep(1); long ticks = DateTime.Now.Ticks; return ConvertToBase(ticks, 36); } } private static String ConvertToBase(long num, int nbase) { String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // check if we can convert to another base if (nbase < 2 || nbase > chars.Length) return ""; long r; String newNumber = ""; // in r we have the offset of the char that was converted to the new base while (num >= nbase) { r = num % nbase; newNumber = chars[(int)r] + newNumber; num = num / nbase; } // the last number to convert newNumber = chars[(int)num] + newNumber; return newNumber.ToLower(); } }
Enjoy !
相关文章推荐
- How to retrieve the Device Unique ID from android device
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- How To Change the Partition Column Of A Partitioned Table Using DBMS_Redefinition [ID 846405.1]
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- How to Restore the Database Using AMDU after Diskgroup Corruption (Doc ID 1597581.1)
- How To Change the Partition Column Of A Partitioned Table Using DBMS_Redefinition [ID 846405.1]
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- CodeSnip: How to Get Id of the Record Using ASP.NET and SQL Server 2000
- How To Download The Latest Updates And Patches Using 11.2.0.2 OUI [ID 1295074.1]
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- How to Pin a Cursor in the Shared Pool using DBMS_SHARED_POOL.KEEP (文档 ID 726780.1)
- How to disable the scheduler using SCHEDULER_DISABLED attribute in 10g (文档 ID 1491941.1)
- How to authenticate against the Active Directory by using Forms authentication and Visual C# .NET
- How To Generate A Wallet Containing A Self Signed Certificate Using ORAPKI [ID 560982.1]
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- How to populate the datagrid on background thread with data binding by using Visual C#