Guid..::.NewGuid Method Home
This page is specific to:.NET Framework Version:1.12.03.03.5Silverlight 34.0
.NET Framework Class Library
Guid..::.NewGuid Method

Initializes a new instance of the Guid class.

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

'Usage

Dim returnValue As Guid

returnValue = Guid.NewGuid()

'Declaration

Public Shared Function NewGuid As Guid

Return Value

Type: System..::.Guid
A new Guid object.
Remarks

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.

Examples

The following code example creates and displays the value 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
'


Platforms

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.
Version Information

.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
See Also

Reference

Community Content

NewGuid method using PowerShell
Added by:Thomas Lee
# 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
Added by:CSharpUniversity.com
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/
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View