In preparation for an upcoming lesson, we’ll want to talk about something called a “GUID.” This acronym stands for “Globally Unique Identifier” and is an alpha-numeric string of characters that is unique to your computer throughout all history. A GUID algorithm takes in a variety of uniquely identifying characteristics, such as your network MAC address, as well as the date, in order to generate your machine’s particular ID. From a .NET developer perspective, GUIDs are often used in databases. For example, whenever you're writing a new record into a database, you would give it a unique GUID. That GUID is then unique to that particular row of data, regardless of whether it's copied elsewhere or if you delete/reinsert rows, you don't have to worry about the ID changing.
Some people would claim that GUIDs are horrible to use for database IDs because they're not necessarily unique. There are situations where you can, theoretically, generate two GUIDs that are exactly the same on two different computers. The chances of that are quite small, unless you're working with a massive set of data.
For more information on GUIDs, how they work, their advantages and disadvantages, check out the link below:https://en.wikipedia.org/wiki/Globally_unique_identifier
To demonstrate GUIDs, create a new ASP.NET project called “CS-ASP_052” with a single resultLabel Control. In Default.aspx.cs, write the following for the Page_Load() method in order to generate a unique GUID:
Notice how GUID is a built-in value type in the .NET Framework. When you run the application, you will get a GUID that is unique to your machine:
You can also parse the string value of a GUID back into a GUID by accessing the static Parse() method:
As with most cases of using a Parse() method, it’s probably better to use TryParse() instead and return the result via an out parameter:
If it were not a valid GUID, it wouldn’t output anything at all:
Now run the application to see if it is, in fact, stored as a valid GUID:
Lesson 52 - Creating GUIDs