Update 5/10/21 - Added Rider
As part of being a developer the likelihood of having to generated a unique identifier of some kind will come to you at some point.
At times there will be a need to create a Guid and there are many ways of doing this and in this blog I am going to go over a few tools which you may or may not know.
First off is the use of SQL. At times when you are writing SQL queries you want to insert a new record into your database table but need a new id. How do you generate this value?
newid() function will create a new unique value for you when the query is executed. Executing the
SELECT statement above will return a unique identifier.
In the .NET world the
Guid struct and it's static factory method
NewGuid() is your bread and butter when you want to create new unique values in code. In the latest .NET 6 console application template it is this simple to output a value to the console window.
One of the easiest ways of generating a unique identifier is to Google "guid generator" and you will be presented with a number of online options which vary in functionality. This has the downside of you don't know if those ids being presented are in fact unique because they could be cached and presented to other internet users. This is unlikely but just a thought.
There are times when you need a unique identifier while you are in Visual Studio, such as when you are creating data for unit tests, and you don't want to create a sample console application or start up SQL Management Studio to create a guid. You don't have to. For a very long time, as long as I can remember, there is a tool built into all versions of Visual Studio which isn't that well known. You access it via
Tools > Create GUID.
After I originally posted this article I was contacted on Twitter by David Henley to tell me about a tool built into JetBrains Rider which also generates guids. Thanks David!
As a developer you will need to generate unique identifier values at some point in your career and in this post we have shown that there are a number of options at your disposal. Some are more obvious than others. For me keeping in your flow and the current application or IDE you're currently in will slow you down less than changing windows etc. I would say I've used the SQL method more in my career when I have needed to script one off data fixes/update scripts etc. However when I write code which when executed needs to create a new unique identifier
Guid.NewGuid() is your friend.
What are other ways to create unique identifiers? What other dev tips do you have? Let me know on Twitter @WestDiscGolf.