How do I use guids in c#8

Time:2021-7-29

Translation link:https://www.infoworld.com/art…

Guid (global unique identifier) is often used in program development. GUID is like the primary key in the database. It can ensure that some very important information, such as customer data or invoice will not be duplicated or data will be overwritten. If there is no guid, it is difficult to ensure data loss and data integrity.

In essence, a GUID is a long string of numbers. From a mathematical point of view, this string of numbers can ensure that it is unique not only in one system, but also in multiple systems and even across systems. In this article, we will discuss why a GUID is needed and how to use a guid in c#8.

To run the examples provided in this article, you need to installVisual Studio 2019, if not, you can download it from the official website.

Why guid

Imagine if you have a sales app mobile app, which can work in both offline and online modes. Suppose that the order ID generated by your program increases from 1. When your application generates some sales order data in the offline state, once connected to the Internet, how to merge these duplicate order IDS? Don’t think there will be conflict, Right, so what you have to do is to completely achieve the uniqueness of the order ID, rather than having to write a lot of code to compromise for this situation.

Next, let’s take a look at how guid solves this problem. GUID is a long number with 128bit bits, which is almost unique. Why should I add it herealmostAnd? Although the GUID is a long number and its conflict is minimal, it does not mean that this possibility is 0.

You can use the guid as the primary key of your data table, so you don’t have to be afraid of primary key conflict when multiple databases are merged, because it won’t happen at all. Moreover, in the offline state, you can use the guid to generate sales orders without connecting to any intranet and extranet.

Composition analysis of guid

The following is an example of a guid. It is worth noting that the GUID is usually 128bit. In order to facilitate reading, it is generally expressed in hexadecimal.


eaa24756-3fac-4e46-b4bb-074ff4f5b846

A GUID is a convention consisting of 32 hexadecimal digits, which are composed of8-4-4-12It is encoded in four blocks. You can carefully compare the guid format above, so there are at most 2 ^ 128 guids.

Use c#8 to generate guids

This section will learn how to use c# to create guids. You can use the structure guid under the system namespace. This is the most direct way to generate guids. You can generate guids in the main method like the following code.


Guid obj = Guid.NewGuid();
Console.WriteLine("The newly created Guid is: " + obj.ToString());
Console.ReadKey();

Use c#8 to generate an empty guid

Because a GUID is a structure, it must be a value type, but you can’t set it to null, which means that if you want to generate an empty guid, you can write as follows:


Guid id = new Guid();
if(id == Guid.Empty)
Console.WriteLine("The Guid is empty");

The value of an empty GUID is:00000000-0000-0000-0000-000000000000, you can use this value to determine whether another GUID is empty, right? The code is as follows:


if (guid != Guid.Empty){  
  //The GUID object contains non-zero values
}
else
{
  //The GUID object is empty
} 

Next, I write a simple extension method to judge whether the specified GUID is empty.


public static bool IsNullOrEmpty(this Guid guid)
{
  return (guid == Guid.Empty);
} 

If you want to determine whether the Guid of an nullable type is null, you can use the following extension method.


public static bool IsNullOrEmpty(this Guid? guid)
{
  if (guid.HasValue)
    if (guid == default(Guid))
        return true;
    return false;
}

It is worth noting that default (GUID) and guid. Empty are equivalent.

How to convert guid to string in c#8

You can even convert a guid to a string value. The following code snippet shows how to convert an empty guid to a string.


string str = Guid.Empty.ToString();
Console.WriteLine(str);

In the process of using guids, it should be noted that in addition to the conflict with minimal probability, you should also pay attention to the fact that guids will occupy more storage space and its generation disorder. In fact, you can also implement them flexibly, such as using two ulons with a total of 128bit and increasing them orderly to realize a unique Guid of programming nature.

If you want to bind the guid to your data control or user interface, you can convert the guid to string, or even toString the guid in the format you want.

You can also generate a guid in other ways, such as random number, time-based, hardware based and content-based (such as MD5 and SHA-1). In the following articles, I will show you how to use these methods to realize and more advanced functions of guid.

More high quality dry goods: see my GitHub:dotnetfly