Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
Guid Structure
Guid Methods
 NewGuid Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Guid..::.NewGuid Method

Initializes a new instance of the Guid class.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Function NewGuid As Guid
Visual Basic (Usage)
Dim returnValue As Guid

returnValue = Guid.NewGuid()
C#
public static Guid NewGuid()
Visual C++
public:
static Guid NewGuid()
JScript
public static function NewGuid() : Guid

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.

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

Visual Basic
' 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
'
C#
// 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

*/

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
NewGuid method using PowerShell      Thomas Lee   |   Edit   |   Show History
# new-guid.ps1
# creates a new GUID
# thomas lee - tfl@psp.co.uk

# Create a new guid and display it
$guid = [system.guid]::newguid()
$guid
This script produces the following output:

PSH [D:\foo]: D:\foo\new-guid.ps1

Guid
----
a55954b3-f858-4f83-82a9-38422b10168d


Don't try to create your own unique identifier-Use the GUID object      CSharpUniversity.com   |   Edit   |   Show History
If you are developing an application and you need to ensure that something like a filename is unique, use a GUID. Don't try to come up with your own unique identifier. You can even use a GUID appended with another string that makes sense in your application. To see an example where a GUID is used to create a unique image file name on the web server when a file is uploaded please see:
http://www.csharpuniversity.com/2009/03/05/uploading-files-and-validating-them-using-the-aspnet-fileupload-and-customvalidator-controls/
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker