System.Guid Struct

Struct Represents a globally unique identifier (GUID).
Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Overview

A GUID is a 128-bit integer used to identify information, nearly universally unique across space and time. GUIDs are often used to uniquely identify software components, interfaces, and objects.

The Guid struct in .NET provides methods for creating, manipulating, and comparing GUIDs.

Constructors

public Guid(byte[] b)

Initializes a new instance of the Guid struct using the specified byte array.

Parameters

  • b: An array of 16 bytes to use for the GUID.
public Guid(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 struct using the specified integer, three shorts, and five bytes.

Parameters

  • a: The most significant 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 least significant byte of the GUID.
public Guid(string g)

Initializes a new instance of the Guid struct using the specified string representation.

The string g must be in one of the following formats:

  • "d{ddddddddddd"
  • "{dddddddd-dddd-dddd-dddd-dddddddddddd"
  • "{d:dddddddd:dddddddd:dddddddddddd"
  • "ddddddddddddddddddd"
  • "{dddddddddddddddddddddddddddddddd"

where d is a hexadecimal digit.

Parameters

  • g: The string representation of the GUID.

Properties

public int Data1 { get; }

Gets the first four bytes of the GUID.

public short Data2 { get; }

Gets the next two bytes of the GUID.

public short Data3 { get; }

Gets the next two bytes of the GUID.

public byte[] Data4 { get; }

Gets the last eight bytes of the GUID.

public static Guid Empty { get; }

Gets a new, empty GUID.

Methods

public int CompareTo(object value)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in sort order as the other object.

Parameters

  • value: An object to compare with this instance.

Returns

  • A value that indicates the relative order of the objects being compared.
public int CompareTo(Guid value)

Compares the current instance with another Guid structure and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in sort order as the other object.

Parameters

  • value: A Guid structure to compare with the current instance.

Returns

  • A value that indicates the relative order of the objects being compared.
public bool Equals(object obj)

Returns a value indicating whether this instance is equal to a specified object.

Parameters

  • obj: An object to compare with this instance.

Returns

  • true if obj is a Guid structure that has the same binary representation as this instance; otherwise, false.
public bool Equals(Guid g)

Returns a value indicating whether this instance is equal to a specified Guid structure.

Parameters

  • g: A Guid structure to compare with this instance.

Returns

  • true if g has the same binary representation as this instance; otherwise, false.
public override int GetHashCode()

Returns the hash code for this instance.

Returns

  • A 32-bit signed integer hash code.
public static Guid NewGuid()

Initializes a new instance of the Guid struct. The value is guaranteed to be unique among all possible values that can be generated.

Returns

  • A new Guid structure.

Example:


using System;

public class Example
{
    public static void Main()
    {
        Guid myGuid = Guid.NewGuid();
        Console.WriteLine($"Generated GUID: {myGuid}");
    }
}
                    
public override string ToString()

Converts the value of this instance to its equivalent string representation.

Returns

  • The string representation of this instance.
public string ToString(string format)

Converts the value of this instance to its equivalent string representation using the specified format.

Parameters

  • format: A string that specifies the format of the GUID. The possible values are 'N', 'D', 'B', 'P', and 'X'.

Returns

  • The string representation of this instance in the specified format.
public static bool TryParse(string s, out Guid result)

Tries to convert the string representation of a GUID to its Guid equivalent. A return value indicates whether the conversion succeeded.

Parameters

  • s: The string to parse.
  • result: When this method returns, contains the parsed GUID, if the conversion succeeded, or Guid.Empty if the conversion failed.

Returns

  • true if s was converted successfully; otherwise, false.
public static bool TryParseExact(string s, string format, out Guid result)

Tries to convert the string representation of a GUID to its Guid equivalent using the specified format. A return value indicates whether the conversion succeeded.

Parameters

  • s: The string to parse.
  • format: The required format of s.
  • result: When this method returns, contains the parsed GUID, if the conversion succeeded, or Guid.Empty if the conversion failed.

Returns

  • true if s was converted successfully; otherwise, false.

Operators

public static bool operator ==(Guid g1, Guid g2)

Compares two Guid structures for equality.

public static bool operator !=(Guid g1, Guid g2)

Compares two Guid structures for inequality.

public static bool operator <(Guid g1, Guid g2)

Compares two Guid structures to determine if the first is less than the second.

public static bool operator <=(Guid g1, Guid g2)

Compares two Guid structures to determine if the first is less than or equal to the second.

public static bool operator >(Guid g1, Guid g2)

Compares two Guid structures to determine if the first is greater than the second.

public static bool operator >=(Guid g1, Guid g2)

Compares two Guid structures to determine if the first is greater than or equal to the second.