NewGuid Method
Collapse the table of content
Expand the table of content

Guid.NewGuid Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Initializes a new instance of the Guid class.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static Guid NewGuid()

Return Value

Type: System.Guid
A new Guid object.

This is a convenient static method that you can call to get a new Guid.

There is a very low probability that the value of the new Guid is all zeroes or equal to any other Guid. You can determine whether a GUID consists of all zeros by comparing it to Guid.Empty.

The following code example creates and displays the values of two Guid objects.


// This code example demonstrates the Guid.NewGuid() method.

using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Guid g;
      // Create and display the value of two GUIDs.
      g = Guid.NewGuid();
      outputBlock.Text += g + "\n";
      outputBlock.Text += Guid.NewGuid() + "\n";
   }
}

/*
This code example produces the following results:

0f8fad5b-d9cb-469f-a165-70867728950e
7c9e6679-7425-40de-944b-e07fc1f90ae7

*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft