Developer's Center
C#

Generate a UUID in C#

C# is a widely adopted programming language known for its versatility and strong integration with the .NET framework. In this article, we will delve into the process of generating UUIDs in C# and provide detailed explanations of the code involved.

C# is a general-purpose, object-oriented, and strongly typed programming language that has gained immense popularity since its inception in 2000. It finds applications in both frontend and backend development and is particularly well-suited for creating Windows-based applications due to its close association with the Microsoft .NET framework.

How to Generate a UUID in C#

C# offers built-in support for generating Version 4 UUIDs through the .NET framework. Below is an example showcasing how to generate a UUID in C#:

using System;
using System.Diagnostics;
 
namespace UUIDExampleApplication {
    class Program {
        static void Main(string[] args) {
            Guid myuuid = Guid.NewGuid();
            string myuuidAsString = myuuid.ToString();
 
            Debug.WriteLine("The UUID is: " + myuuidAsString);
        }
    }
}

Explanation

  • Line #7 utilizes the Guid.NewGuid() static method from the System namespace to create a new Guid instance, myuuid. In the .NET framework, a Guid is equivalent to a UUID.
  • Line #8 converts the Guid instance to a string representation using the ToString() method. The resulting string follows the standard UUID format (e.g., 2e63341a-e627-48ac-bb1a-9d56e2e9cc4f). When storing the UUID in a file, database, model property, or transmitting it via an API call, the string representation is typically used instead of the Guid instance itself.
  • The output displayed on line #10 will resemble the following:
The UUID is: 2e63341a-e627-48ac-bb1a-9d56e2e9cc4f

Converting a String to a UUID

In certain scenarios, you may need to convert a string representation of a UUID back into a Guid instance. Although this situation is uncommon, C# provides a suitable mechanism for achieving this. Consider the following example:

using System;
using System.Diagnostics;
 
namespace UUIDExampleApplication {
    class Program {
        static void Main(string[] args) {
            Guid myuuid = Guid.NewGuid();
            string myuuidAsString = myuuid.ToString();
 
            Guid sameUuid = new Guid(myuuidAsString);
            Debug.Assert(sameUuid.Equals(myuuid));
        }
    }
}

Explanation

  • Line #10 demonstrates the conversion of the string representation of a UUID into a Guid instance (sameUuid) using the Guid(String) constructor.
  • Line #11 verifies the equality of the two Guid instances.

Conclusion

In this comprehensive guide, we have explored the process of generating UUIDs in C# using the powerful capabilities of the .NET framework. By understanding the code snippets and conversion methods presented here, you can confidently incorporate UUID generation into your C# applications. UUIDs play a vital role in various scenarios, enabling unique identification and preventing collisions in your projects.