Updated: August 2011
Initializes a new instance of the Guid structure.
Assembly: mscorlib (in mscorlib.dll)
Public Shared Function NewGuid As Guid
public static Guid NewGuid()
public: static Guid NewGuid()
static member NewGuid : unit -> Guid
This is a convenient static method that you can call to get a new Guid.
The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low. You can determine whether a GUID consists of all zeros by comparing it to Guid.Empty.
The following example creates and displays the values of two Guid objects.
' This code example demonstrates the Guid.NewGuid() method. Imports System Class Sample Public Shared Sub Main() Dim g As Guid ' Create and display the value of two GUIDs. g = Guid.NewGuid() Console.WriteLine(g) Console.WriteLine(Guid.NewGuid()) End Sub 'Main End Class 'Sample ' 'This code example produces the following results: ' '0f8fad5b-d9cb-469f-a165-70867728950e '7c9e6679-7425-40de-944b-e07fc1f90ae7 '
// This code example demonstrates the Guid.NewGuid() method. using System; class Sample { public static void Main() { Guid g; // Create and display the value of two GUIDs. g = Guid.NewGuid(); Console.WriteLine(g); Console.WriteLine(Guid.NewGuid()); } } /* This code example produces the following results: 0f8fad5b-d9cb-469f-a165-70867728950e 7c9e6679-7425-40de-944b-e07fc1f90ae7 */
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Portable Class Library
Supported in: Portable Class LibraryWindows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Reference
|
Date |
History |
Reason |
|---|---|---|
|
August 2011 |
Expanded the Remarks section. |
Customer feedback. |
I actually think the statement: "The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low.The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low." is very ambiguous.
My understanding is that the NewGuid method on a machine with a Unique MAC address should produce a Guid that is globally unique. Seeing the word "Very Low" is is a bit troubling.
What Is Very Low?
The basic problem is that the global uniqueness of GUIDs across time and space cannot be guaranteed with complete certainty; there is an extremely small possibility of duplication. The documentation uses the phrase "very low". It might have used some other phrase, like "statistically insignificant" or "neglibile" or "virtually non-existent". But neverthless, there is an extremely small probability that duplicate GUIDs can be generated.
--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation
How can we custom GUID generation of System.Guid.NewGuid() ?
Is there a GuidProvider we can use or program ?
Custom GUID Generation
No, the Guid structure does not support custom GUID generation; you cannot implement a GUID provider that will be called by the static NewGuid method. If you want to add this functionality, you can do it either with an extension method (which will then appear as a static method of the Guid structure, such as Guid.CustomGuid), or with an independent implementation of a GUID that can wrap some of the existing functionality of the Guid structure, such as its parsing and formatting methods.
I hope that this helps to answer your question.
--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation
NewGuid is a static method, so use the "::" characters in the method call.
PS C:\> [System.Guid]::NewGuid()
Guid
----
7863cacd-a5f1-4941-8c25-8702ab62e799
The GUID object has a GUID property, so to get only that property, type:
PS C:\> [System.Guid]::NewGuid().Guid
7863cacd-a5f1-4941-8c25-8702ab62e799
To get an all-zeros GUID, type:
PS C:\> New-Object System.GUID
Guid
----
00000000-0000-0000-0000-000000000000
Here's a simple function that generates a GUID whenever you need one:
function New-Guid { [System.Guid]::NewGuid().Guid }
To use it, just type "New-Guid".
June Blender
Senior Programming Writer
Windows PowerShell
As part of the GUID originates from the computer hardware, it should not be possible for an all Fs GUID to be created unless the hardware itself was to generate a constant that resulted in all Fs.
Creating a GUID with Specific Digits
It is possible to create a GUID containing all Fs (or any other specified hexadecimal digits, for that matter). The Guid.NewGuid method creates a new GUID with values that depend on the system clock. In contrast, each of the constructors of the GUID structure allow you to create a GUID in which you define all 32 hexadecimal digits of the GUID.
--Ron Petrusha
Common Language Runtime Developer Content
Microsoft Corporation
I am using the all-zeros GUID to represent a "null" GUID, and the all F's GUID to represent the maximum GUID that can be generated. I've searched on the Internet and it appears that Guid.NewGuid cannot generate the "null" GUID, which is already provided by Guid.Empty; however, what about an all-F's GUID?
An All Fs GUID
I'm not quite sure what your scenario is, but if you need to predictably generate a GUID that contains all Fs, the best way to do it is by calling one of the overloads of the GUID constructor. The best choice is new Guid(new String('F', 32)) (in C#) or New Guid(New String("F"c, 32)) (in Visual Basic).
--Ron Petrusha
CLR Developer Content Team
Microsoft Corporation