C# Guid ToByteArray

Description

Guid ToByteArray returns a 16-element byte array that contains the value of this instance.

Syntax

Guid.ToByteArray has the following syntax.


public byte[] ToByteArray()

Returns

Guid.ToByteArray method returns

Example

The following example calls the NewGuid method to create a Guid value, and then calls the ToByteArray method to represent the Guid value as a byte array.


//from  w  w w .  j  a v  a2 s.c  o  m
using System;

public class Example
{
   public static void Main()
   {
      Guid guid = Guid.NewGuid();
      Console.WriteLine("Guid: {0}", guid);
      Byte[] bytes = guid.ToByteArray();
      foreach (var byt in bytes){
         Console.Write("{0:X2} ", byt);
      }
      Guid guid2 = new Guid(bytes);
      Console.WriteLine("Guid: {0} (Same as First Guid: {1})", guid2, guid2.Equals(guid));
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version