Generating and working with GUIDs in .NET

 

Takeaway: A Globally Unique Identifier (GUID) is a 128-bit integer that you can use across all computers and networks wherever a unique identifier is required. Tony Patton explains how the .NET Framework makes it possible for you to create your own GUID.

TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!

When Windows developers need a unique value, they often utilize a Globally Unique Identifier (GUID). Microsoft uses the term GUID for a unique number that identifies an entity, such as a Word document.

A GUID is a 128-bit integer (16 bytes) that you can use across all computers and networks wherever a unique identifier is required. There's a very low probability that this type of identifier will be duplicated.

This article explains how the .NET Framework makes it possible for you to create your own GUID.

Everywhere you look

GUIDs are used throughout the Windows environment. When you peruse the registry on a Windows system, you can see that GUIDs are used extensively to uniquely identify applications and so forth. In particular, they serve as application IDs under the HKEY_CLASSES_ROOT section (AppID key).

This is the format of a typical GUID:

936DA01F-9ABD-4d9d-80C7-02AF85C822A8

Generating a GUID with .NET

Possessing a unique identifier makes it easy to store and retrieve information. This is especially useful when working with a database because a GUID makes an excellent primary key.

Also, SQL Server integrates well with GUID usage. The SQL Server data type uniqueidentifier stores a GUID value. You can either generate this value within SQL Server—using the NEWID() function—or you can generate the GUID outside of SQL Server and insert it manually.

The latter approach is a straightforward process in .NET. The base System class in the .NET Framework includes the GUID value type. In addition, this value type includes methods to work with GUID values. In particular, the NewGUID method allows you to easily generate a new GUID.

The following C# command-line application shows how it's used:

using System;
namespace DisplayGUID {
class GuidExample {
static void Main(string[] args) {
GenerateGUID();
}
static void GenerateGUID() {
Console.WriteLine("GUID: " + System.Guid.NewGuid().ToString());
} } }

Here's the output of the program (though the GUID will vary from system to system):

GUID: 9245fe4a-d402-451c-b9ed-9c1a04247482

This is the same code using VB.NET:

Module BuilderExamples
Sub Main()
GenerateGUID()
End Sub
Public Sub GenerateGUID()
Console.WriteLine("GUID: " + System.Guid.NewGuid().ToString())
End Sub
End Module

And here's the same code example using J#:

package BuilderExamples;
import System.Console;
public class GUIDExample {
public GUIDExample() { }
public static void main(String[] args) {
GenerateGUID();
}
static void GenerateGUID() {
Console.WriteLine("GUID: " + System.Guid.NewGuid().ToString());
} }

The example code uses the NewGuid function of the System.Guid namespace to return a value. (If you ever had to do this in Visual Basic, you'll appreciate the simplicity of this approach.)

At this point, you may be thinking that GUIDs are a nice feature, but how and where would you use them in your applications?

Using a GUID in your application

A GUID makes a great primary key in a back-end database. The example in Listing A uses a GUID to store information in a back-end database, which has the following columns: pk_guid (uniqueidentifier data type) and name (nvarchar data type). A simple Windows form is presented with one text box. The data from the text box is inserted into the database table when a button is selected. A GUID is generated by the application code and stored in the other column.

Another GUID application is assigning a unique identifier to a .NET class or interface; that is, the GUID is assigned as an attribute for the class or interface. This is accomplished using the standard attribute syntax.

We can extend our first example to assign a GUID to it. The System.Runtime.InteropServices namespace must be referenced to utilize the GUID attribute. The following C# code does that:

using System;
using System.Runtime.InteropServices;
namespace DisplayGUID {
[Guid("9245fe4a-d402-451c-b9ed-9c1a04247482")]
class GuidExample {
static void Main(string[] args) {
GenerateGUID();
}
static void GenerateGUID() {
Console.WriteLine("GUID: " + System.Guid.NewGuid().ToString());
} } }

GUIDs have never been easier

As with most aspects of development, the .NET Framework simplifies the process of creating and working with GUID values. This makes it easy to generate unique values where necessary in your .NET application.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值