How to create Guid value in C#?

CsharpServer Side ProgrammingProgramming

A Globally Unique Identifier or GUID represents a gigantic identification number — a number so large that it is mathematically guaranteed to be unique not only in a single system like a database, but across multiple systems or distributed applications.

The total number of unique keys (3.40282366×1038) is so large that the probability of the same number being generated twice is very small. For an application using 10 billion random GUIDs, the probability of a coincidence is approximately 1 in a quintillion.(1030)

For example, in Retail domain if we want to generate a unique for each transaction so that a customer can use that id to do post sale operations like return, adjustment etc., GUID can be used. GUIDs are most commonly written in text as a sequence of hexadecimal digits like 3F2504E0-4F89-11D3-9A0C-0305E82C3301.

Guid is present in System namespace in C#. It can be created like below.

Guid demoGuid = Guid.NewGuid();

Example

 Live Demo

using System;
namespace DemoApplication{
   class Program{
      static void Main(string[] args){
         Guid demoGuid = Guid.NewGuid();
         Console.WriteLine(demoGuid);
         Console.WriteLine(Guid.NewGuid());
         Console.ReadLine();
      }
   }
}

Output

The output of the above code is

3a251d82-e8ce-442f-9e42-5285653a5e8a
09081b06-26e2-49fa-8e96-93748a99defa
Each time when Guid.NewGuid() is called it will generate a random unique guid.
raja
Published on 04-Aug-2020 07:31:14
Advertisements