System.Guid Structure

Assembly: Mscorlib.dll
Namespace: System
Summary
Represents a globally unique identifier (GUID).
C# Syntax:
[Serializable]
public struct Guid : IFormattable, IComparable
Remarks
A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.
See also:
System Namespace

System.Guid Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(byte[] b)

Initializes a new instance of the Guid class using the specified array of bytes.
ctor #2 Overloaded:
.ctor(string g)

Initializes a new instance of the Guid class using the value represented by the specified string.
ctor #3 Overloaded:
.ctor(int a, short b, short c, byte[] d)

Initializes a new instance of the Guid class using the specified integers and byte array.
ctor #4 Overloaded:
.ctor(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)

Initializes a new instance of the Guid class using the specified integers and bytes.
ctor #5 Overloaded:
.ctor(uint a, ushort b, ushort c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)

Initializes a new instance of the Guid class using the specified unsigned integers and bytes.
Public Fields
Empty Initializes a new instance of the Guid class.
Public Methods
CompareTo Compares this instance to a specified object and returns an indication of their relative values.
Equals Overridden:
Returns a value indicating whether this instance is equal to a specified object.
GetHashCode Overridden:
Returns the hash code for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
NewGuid Initializes a new instance of the Guid class.
ToByteArray Returns a 16-element byte array that contains the value of the GUID.
ToString Overloaded:
ToString()

Overridden:
Returns a String representation of the value of this instance in Registry format.
ToString Overloaded:
ToString(string format)

Returns a String representation of the value of this Guid instance, according to the provided format specifier.
ToString Overloaded:
ToString(string format, IFormatProvider provider)

Returns a String representation of the value of this instance of the Guid class, according to the provided format specifier and culture-specific format information.
Public Operators and Type Conversions
op_Equality Returns an indication whether the values of two specified Guid objects are equal.
op_Inequality Returns an indication whether the values of two specified Guid objects are not equal.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Guid Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the Guid class using the specified array of bytes.
C# Syntax:
public Guid(
   byte[] b
);
Parameters:

b

A 16 element byte array containing values with which to initialize the GUID.

Exceptions
Exception Type Condition
ArgumentNullException b is null.
ArgumentException b is not 16 bytes long.

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the Guid class using the value represented by the specified string.
C# Syntax:
public Guid(
   string g
);
Parameters:

g

A String that contains a GUID in the following format: hexadecimal digits are arranged in groups of 8, 4, 4, 4, and 12 digits with hyphens between the groups. The GUID can optionally be enclosed in matching braces. For example: dddddddd-dddd-dddd-dddd-dddddddddddd or {dddddddd-dddd-dddd-dddd-dddddddddddd}.

Alternatively, the following format is permitted: {0xdddddddd,0xdddd, 0xdddd,{0xdd},{0xdd},{0xdd},{0xdd},{0xdd},{0xdd},{0xdd},{0xdd}}, where d is a hexadecimal digit. If this format is used, all brackets and commas indicated are required, and all numbers must be prefixed with "0x" as shown. Fewer hexadecimal digits than shown can be used, but no more.

A String that contains a GUID in the following format: hexadecimal digits are arranged in groups of 8, 4, 4, 4, and 12 digits with hyphens between the groups. The GUID can optionally be enclosed in matching braces. For example: dddddddd-dddd-dddd-dddd-dddddddddddd or {dddddddd-dddd-dddd-dddd-dddddddddddd}.

Alternatively, the following format is permitted: {0xdddddddd,0xdddd, 0xdddd,{0xdd},{0xdd},{0xdd},{0xdd},{0xdd},{0xdd},{0xdd},{0xdd}}, where d is a hexadecimal digit. If this format is used, all brackets and commas indicated are required, and all numbers must be prefixed with "0x" as shown. Fewer hexadecimal digits than shown can be used, but no more.

Exceptions
Exception Type Condition
ArgumentNullException g is null.
FormatException The length of g is invalid. The length should be 38 if(-)'{' and(-)'}' are used, and 36 otherwise (counting digits and dashes).

The first (or last) character in g is(-)'{' but the last (or first) character is not(-)'}'.

g contains(-)'-' in the wrong places. The expected format is 8 hexadecimal digits followed by a dash, followed by 4 digits then a dash, followed by 4 digits then a dash, followed by 4 digits then a dash, followed by 12 digits.

g contains improperly grouped hexadecimal digits. The expected format is 8 hexadecimal digits followed by a dash, followed by 4 digits then a dash, followed by 4 digits then a dash, followed by 4 digits then a dash, followed by 12 digits.

An element in g does not represent a valid hexadecimal number.

Remarks
The string can begin and end with braces: '{', and '}'.
Example
The following strings are valid for g:

"CA761232-ED42-11CE-BACD-00AA0057B223"

or

"{CA761232-ED42-11CE-BACD-00AA0057B223}"

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the Guid class using the specified integers and byte array.
C# Syntax:
public Guid(
   int a,
   short b,
   short c,
   byte[] d
);
Parameters:

a

The first 4 bytes of the GUID.

b

The next 2 bytes of the GUID.

c

The next 2 bytes of the GUID.

d

The remaining 8 bytes of the GUID.

Exceptions
Exception Type Condition
ArgumentNullException d is null.
ArgumentException d is not 8 bytes long.
Example
Guid(1,2,3,new byte[]{0,1,2,3,4,5,6,7}) creates a Guid that corresponds to "00000001-0002-0003-0001-020304050607".

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the Guid class using the specified integers and bytes.
C# Syntax:
public Guid(
   int a,
   short b,
   short c,
   byte d,
   byte e,
   byte f,
   byte g,
   byte h,
   byte i,
   byte j,
   byte k
);
Parameters:

a

The first 4 bytes of the GUID.

b

The next 2 bytes of the GUID.

c

The next 2 bytes of the GUID.

d

The next byte of the GUID.

e

The next byte of the GUID.

f

The next byte of the GUID.

g

The next byte of the GUID.

h

The next byte of the GUID.

i

The next byte of the GUID.

j

The next byte of the GUID.

k

The next byte of the GUID.

Remarks
Specifying individual bytes in this manner can be used to circumvent byte order restrictions (big-endian or little-endian byte order) on particular types of computers.
Example
Guid(0xa,0xb,0xc,0,1,2,3,4,5,6,7) creates a Guid that corresponds to "0000000a-000b-000c-0001-020304050607".

Return to top


Overloaded ctor #5
Summary
Initializes a new instance of the Guid class using the specified unsigned integers and bytes.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public Guid(
   uint a,
   ushort b,
   ushort c,
   byte d,
   byte e,
   byte f,
   byte g,
   byte h,
   byte i,
   byte j,
   byte k
);
Parameters:

a

The first 4 bytes of the GUID.

b

The next 2 bytes of the GUID.

c

The next 2 bytes of the GUID.

d

The next byte of the GUID.

e

The next byte of the GUID.

f

The next byte of the GUID.

g

The next byte of the GUID.

h

The next byte of the GUID.

i

The next byte of the GUID.

j

The next byte of the GUID.

k

The next byte of the GUID.

Remarks
Specifying the bytes in this manner avoids endianness issues.
Example
Guid(0xa,0xb,0xc,0,1,2,3,4,5,6,7) creates a Guid that corresponds to "0000000a-000b-000c-0001-020304050607".

Return to top


Field: Empty
Summary
Initializes a new instance of the Guid class.
C# Syntax:
public static readonly Guid Empty;

Return to top


Method: CompareTo(
   object value
)
Summary
Compares this instance to a specified object and returns an indication of their relative values.
C# Syntax:
public int CompareTo(
   object value
);
Parameters:

value

An object to compare, or null.

Return Value:
A signed number indicating the relative values of this instance and value.

Value Description
A negative integer This instance is less than .
Zero This instance is equal to .
A positive integer This instance is greater than value. -or- value is null.
Exceptions
Exception Type Condition
ArgumentException value is not a Guid.
Remarks
Any instance of Guid, regardless of its value, is considered greater than null.

value must be null or an instance of Guid; otherwise, an exception is thrown.

Return to top


Overridden Method: Equals(
   object o
)
Summary
Returns a value indicating whether this instance is equal to a specified object.
C# Syntax:
public override bool Equals(
   object o
);
Parameters:

o

The object to compare with this instance.

Return Value:
true if o is a Guid that has the same value as this instance; otherwise, false.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~Guid();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetHashCode()
Summary
Returns the hash code for this instance.
C# Syntax:
public override int GetHashCode();
Return Value:
The hash code for this instance.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: NewGuid()
Summary
Initializes a new instance of the Guid class.
C# Syntax:
public static Guid NewGuid();
Return Value:
A new Guid object.
Remarks
This is a convenient static method that you can call to get a new Guid.

Return to top


Method: op_Equality(
   Guid a,
   Guid b
)
Summary
Returns an indication whether the values of two specified Guid objects are equal.
C# Syntax:
public static bool operator ==(
   Guid a,
   Guid b
);
Parameters:

a

A Guid object.

b

A Guid object.

Return Value:
true if a and b are equal; otherwise, false.

Return to top


Method: op_Inequality(
   Guid a,
   Guid b
)
Summary
Returns an indication whether the values of two specified Guid objects are not equal.
C# Syntax:
public static bool operator !=(
   Guid a,
   Guid b
);
Parameters:

a

A Guid object.

b

A Guid object.

Return Value:
true if a and b are not equal; otherwise, false.

Return to top


Method: ToByteArray()
Summary
Returns a 16-element byte array that contains the value of the GUID.
C# Syntax:
public byte[] ToByteArray();
Return Value:
A 16-element byte array.

Return to top


Overloaded Method: ToString()
Summary
Returns a String representation of the value of this instance in Registry format.
C# Syntax:
public override string ToString();
Return Value:
A String formatted in this pattern:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

where the value of the GUID is represented as a series of lower-case hexadecimal digits in groups of 8, 4, 4, 4, and 12 digits and separated by hyphens. An example of a return value is "382c74c3-721d-4f34-80e5-57657b6cbc27".

Remarks
This method provides a default GUID format that is sufficient for typical use; however, other versions of this method that take a format parameter provide a few common format variations.

Return to top


Overloaded Method: ToString(
   string format
)
Summary
Returns a String representation of the value of this Guid instance, according to the provided format specifier.
C# Syntax:
public string ToString(
   string format
);
Parameters:

format

A String containing a single format specifier character indicating how the GUID value should be formatted.

Return Value:
A String representation of the value of this Guid instance.
Remarks
format can contain the following format specifiers. In the table that follows, all digits in the return value are hexadecimal. Each character 'x' represents a hexadecimal digit; each hyphen ('-'), bracket ('{', '}'), and parenthesis ('(', ')') appears as shown.

Specifier Format of Return Value
N 32 digits: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
D 32 digits separated by hyphens: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
B 32 digits separated by hyphens, enclosed in brackets: {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
P 32 digits separated by hyphens, enclosed in parentheses: (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

Return to top


Overloaded Method: ToString(
   string format,
   IFormatProvider provider
)
Summary
Returns a String representation of the value of this instance of the Guid class, according to the provided format specifier and culture-specific format information.
C# Syntax:
public string ToString(
   string format,
   IFormatProvider provider
);
Parameters:

format

A String containing a single format specifier character indicating how the GUID value should be formatted.

provider

(Reserved) An IFormatProvider reference that supplies culture-specific formatting services.

Return Value:
A String representation of the value of this Guid instance.
Exceptions
Exception Type Condition
FormatException The value of format is not "N", "D", "B", or "P".
Remarks
format can contain the following format specifiers. In the table that follows, all digits in the return value are hexadecimal. Each character 'x' represents a hexadecimal digit; each hyphen ('-'), bracket ('{', '}'), and parenthesis ('(', ')') appears as shown.

Specifier Format of Return Value
N 32 digits: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
D 32 digits separated by hyphens: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
B 32 digits separated by hyphens, enclosed in brackets: {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
P 32 digits separated by hyphens, enclosed in parentheses: (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

If format is null or the empty string, the 'N' format specifier is assumed.

The provider parameter is reserved for future use and does not contribute to the execution of this method. A null reference can be coded for this parameter.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.