473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Alternatives to "Guid"

Hi there,

I need to create a unique identifier for my own internal needs and am happy
to rely on "Guid.NewGuid()" to pull this off. I'd like to know if .NET
offers any competing alternative however. Perhaps there's something with a
richer set of functions for instance and it might offer potential advantages
over a GUID. Am just exploring the possibilities for now. Thanks in advance.
Apr 27 '07 #1
8 8679
You haven't qualified the requirement very much...

If it for internal purposes, then a simple integer / long incremented
in a thread-safe manner (either lock or interlocked) would suffice
(although you haven't mentioned volume). Guid works for most other
scenarios. Avoid DateTime based solutions; the resolution is simply
not high enough for volume work. If you want bona-fide (verified)
uniqueness over either a persistant system or a cluster or servers,
then a database is the way to go; either with an identity column (if
guessing isn't a security issue), or a GUID with a unique constraint.

What "richer set of functions" are you looking for?

Marc

Apr 27 '07 #2
..NET Framework does not, to my knowledge, offer any "built-in" alternatives
to Guid. It really depends on "how unique" you want your numbers to be. A
Guid is a 128 bit number and the chances of a duplicate ever being generated
-- not just in your lifetime -- but EVER - is 1 in the billions.

If you want shorter numbers or strings that will be cryptographically unique
but not as "globally unique" as a Guid, you could use code like this:

private string GetUniqueKey()
{
int maxSize = 8 ;
int minSize = 5 ;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890";
chars = a.ToCharArray();
int size = maxSize ;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data) ;
size = maxSize ;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size) ;
foreach(byte b in data )
{ result.Append(chars[b % (chars.Length - 1)]); }
return result.ToString();
}

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jack Brown" wrote:
Hi there,

I need to create a unique identifier for my own internal needs and am happy
to rely on "Guid.NewGuid()" to pull this off. I'd like to know if .NET
offers any competing alternative however. Perhaps there's something with a
richer set of functions for instance and it might offer potential advantages
over a GUID. Am just exploring the possibilities for now. Thanks in advance.
Apr 27 '07 #3
If you want shorter numbers or strings that will be cryptographically
unique
but not as "globally unique" as a Guid, you could use code like this:

private string GetUniqueKey()
{
int maxSize = 8 ;
int minSize = 5 ;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890";
chars = a.ToCharArray();
int size = maxSize ;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data) ;
size = maxSize ;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size) ;
foreach(byte b in data )
{ result.Append(chars[b % (chars.Length - 1)]); }
return result.ToString();
Thanks very much. I'll take a look at this and "RNGCryptoServiceProvider" in
particular to see what it offers. Thanks again.
Apr 27 '07 #4
Thanks for the feeback.
You haven't qualified the requirement very much...
I need to store a unique value in a (DataSet) file I'm creating. Each time
the file is created via "DataSet.WriteXml(), a unique ID will be stored in
one of the tables. This way, any and all copies of the file will contain the
same unique ID (so they're all linked via this ID).
If it for internal purposes, then a simple integer / long incremented
in a thread-safe manner (either lock or interlocked) would suffice
(although you haven't mentioned volume). Guid works for most other
scenarios. Avoid DateTime based solutions; the resolution is simply
not high enough for volume work. If you want bona-fide (verified)
uniqueness over either a persistant system or a cluster or servers,
then a database is the way to go; either with an identity column (if
guessing isn't a security issue), or a GUID with a unique constraint.
A random integer or the date/time actually works very well in practice.
Chances of a collision is very remote depending on the volume of activty (as
you pointed out). It will likely work in my case but space is cheap so a
GUID is clearly preferable.
What "richer set of functions" are you looking for?
I was just exploring the possibilities. If there was an alternative then it
might have offered something that could have proved useful either now or
later. Sometimes new and better ideas surface when you explore the latest
APIs. Anyway, thanks again.
Apr 27 '07 #5
Fair enough; personally, in this scenario I'd stick with guid and move
on ;-p

Marc

Apr 27 '07 #6
Fair enough; personally, in this scenario I'd stick with guid and move
on ;-p
And I probably will. I find it takes me twice as long to develop in .NET
compared to the WinAPI so I have little time to waste. Thanks again.
Apr 27 '07 #7
You genuinely surprise me. I've always found it the other way. In
particular, it takes me significantly less time to *debug*,
particularly if I stick mostly to managed classes.

Marc

Apr 28 '07 #8
On Sat, 28 Apr 2007 13:49:14 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
You genuinely surprise me. I've always found it the other way. In
particular, it takes me significantly less time to *debug*,
particularly if I stick mostly to managed classes.
But he wrote "develop", not "debug".

Fact is, I can relate. I've only been doing .NET stuff for about a year.
I've been doing Windows programming for almost twenty. I find that much
of my time writing .NET stuff is spent poring over the documentation
trying to figure out just what .NET class, method, and/or property I need
to use in order to do what I want. If I were doing the same thing in the
native Windows API, I would already know how to do what I want, and mostly
I'd just have to type it in.

Obviously this will change over time, as I become more familiar with
..NET. And obviously for someone with enough experience in .NET
programming, development can go much more quickly. Certainly for me, I
expect that once I've had a lot more experience writing .NET, development
will go much more quickly than the native Windows programming, since I
have found that writing native Windows stuff I spend an inordinate amount
of time just hooking up the UI, implementing all that stuff that goes into
a window proc. .NET has lots of other time-saving stuff in it as well,
but frankly the native Windows API has IMHO done a decent job of
simplifying a lot of the other stuff already. It's mostly in the UI stuff
that very little has changed and been streamlined over the years, and so
the ease with which a user-interface can be put together in .NET is a huge
boon and time-saver.

But for now, because of how hard it is to find what one's looking for so
often, I find .NET development goes fairly slowly a lot of the time,
relative to what would be possible for me in native Windows.

Maybe that's what he meant. :)

Pete

(sometimes I wish the .NET documentation had some kind of native Windows
cross-reference, where I could just look up the function I'd use in the
native Windows API and it would tell me the equivalent in .NET. Heck, if
they'd just add a .NET link to each of the regular Windows API doc pages,
that would be good enough for me).
Apr 29 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Arun Bhalla | last post by:
I'm borrowing some code from Pavel's Command Prompt Explorer Bar installer to use in my own explorer bar's installer. Recently I've been thinking that using an assembly version like "1.0.*"...
11
by: Ayende Rahien | last post by:
I've a really strange problem, in some part of my code I compare two strings (through object), and while I *know* that they equal each other, and in the watch window they do equal each other, then...
8
by: Paw Pedersen | last post by:
Is there a way to save a variabel that can be access from a static method? I hope there would be some way to save it in memory so I don't have to save it in a file. It's only for a few minutes the...
1
by: Omer kamal | last post by:
When in database(.mdb) the primary key have fields size type "Replication ID" it creates Guid. I tried to create string of Guid and insert it but did it not work. Control.databindings.add(),...
0
by: Michael Fay in SB | last post by:
A month ago I posted: "I built my application with Access 2003 and made a distribution using the Microsoft Office Access 2003 Developer Extensions. (I also install Jet 8 as a follow-on action at...
24
by: M O J O | last post by:
Hi, Instead of doing this.... Public Class Form1 Public Shared Sub CreateAndShow() Dim f As New Form1 f.Show() End Sub
5
by: Aneesh Pulukkul[MCSD.Net] | last post by:
How to convert a "Non Serializable" object to byte array. The object is a dynamically created Excel workbook. As per my understanding an object can be written and read from a stream Only if it's...
2
by: Ronald S. Cook | last post by:
Given that PenID is a Guid, is there a better way to write this? if (PenID.ToString() != "00000000-0000-0000-0000-000000000000") I thought I could do some sort of Guid.Empty check or...
5
by: Andy B | last post by:
I am making a web application that would randomly create an activation code and put it in a database. The web application would then send it in an email to the user with a link to go to and enter...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.