# Class ByteString (3.27.1)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [3.27.1 (latest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString)
- [3.23.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.ByteString)
- [3.15.8](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.ByteString)

    public sealed class ByteString : IEnumerable<byte>, IEnumerable, IEquatable<ByteString>

Immutable array of bytes.

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> ByteString

## Implements

[IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1)[byte](https://learn.microsoft.com/dotnet/api/system.byte), [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.ienumerable), [IEquatable](https://learn.microsoft.com/dotnet/api/system.iequatable-1)[ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString)

## Inherited Members

[object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Protobuf](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf)

## Assembly

Google.Protobuf.dll

## Properties

### Empty

    public static ByteString Empty { get; }

Returns an empty ByteString.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` |   |

### IsEmpty

    public bool IsEmpty { get; }

Returns `true` if this byte string is empty, `false` otherwise.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` |   |

### this\[int\]

    public byte this[int index] { get; }

Returns the byte at the given index.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `index` | `https://learn.microsoft.com/dotnet/api/system.int32` |

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.byte` |   |

### Length

    public int Length { get; }

Returns the length of this ByteString in bytes.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` |   |

### Memory

    public ReadOnlyMemory<byte> Memory { get; }

Provides read-only access to the data of this [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString).
No data is copied so this is the most efficient way of accessing.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.readonlymemory-1https://learn.microsoft.com/dotnet/api/system.byte` |   |

### Span

    public ReadOnlySpan<byte> Span { get; }

Provides read-only access to the data of this [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString).
No data is copied so this is the most efficient way of accessing.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.readonlyspan-1https://learn.microsoft.com/dotnet/api/system.byte` |   |

## Methods

### CopyFrom(params byte\[\])

    public static ByteString CopyFrom(params byte[] bytes)

Constructs a [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from the given array. The contents
are copied, so further modifications to the array will not
be reflected in the returned ByteString.
This method can also be invoked in `ByteString.CopyFrom(0xaa, 0xbb, ...)` form
which is primarily useful for testing.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `bytes` | `https://learn.microsoft.com/dotnet/api/system.byte` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` |   |

### CopyFrom(byte\[\], int, int)

    public static ByteString CopyFrom(byte[] bytes, int offset, int count)

Constructs a [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from a portion of a byte array.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `bytes` | `https://learn.microsoft.com/dotnet/api/system.byte` |
| `offset` | `https://learn.microsoft.com/dotnet/api/system.int32` |
| `count` | `https://learn.microsoft.com/dotnet/api/system.int32` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` |   |

### CopyFrom(ReadOnlySpan\<byte\>)

    public static ByteString CopyFrom(ReadOnlySpan<byte> bytes)

Constructs a [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from a read only span. The contents
are copied, so further modifications to the span will not
be reflected in the returned [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `bytes` | `https://learn.microsoft.com/dotnet/api/system.readonlyspan-1https://learn.microsoft.com/dotnet/api/system.byte` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` |   |

### CopyFrom(string, Encoding)

    public static ByteString CopyFrom(string text, Encoding encoding)

Creates a new [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) by encoding the specified text with
the given encoding.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `text` | `https://learn.microsoft.com/dotnet/api/system.string` |
| `encoding` | `https://learn.microsoft.com/dotnet/api/system.text.encoding` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` |   |

### CopyFromUtf8(string)

    public static ByteString CopyFromUtf8(string text)

Creates a new [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) by encoding the specified text in UTF-8.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `text` | `https://learn.microsoft.com/dotnet/api/system.string` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` |   |

### CopyTo(byte\[\], int)

    public void CopyTo(byte[] array, int position)

Copies the entire byte array to the destination array provided at the offset specified.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `array` | `https://learn.microsoft.com/dotnet/api/system.byte` |
| `position` | `https://learn.microsoft.com/dotnet/api/system.int32` |

### CreateCodedInput()

    public CodedInputStream CreateCodedInput()

Creates a CodedInputStream from this ByteString's data.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream` |   |

### Equals(ByteString)

    public bool Equals(ByteString other)

Compares this byte string with another.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `other` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` The [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) to compare this with. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` | `true` if `other` refers to an equal byte string; `false` otherwise. |

### Equals(object)

    public override bool Equals(object obj)

Compares this byte string with another object.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `obj` | `https://learn.microsoft.com/dotnet/api/system.object` The object to compare this with. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` | `true` if `obj` refers to an equal [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString); `false` otherwise. |

**Overrides** [object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object))

### FromBase64(string)

    public static ByteString FromBase64(string bytes)

Constructs a [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from the Base64 Encoded String.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `bytes` | `https://learn.microsoft.com/dotnet/api/system.string` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` |   |

### FromStream(Stream)

    public static ByteString FromStream(Stream stream)

Constructs a [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from data in the given stream, synchronously.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `stream` | `https://learn.microsoft.com/dotnet/api/system.io.stream` The stream to copy into a ByteString. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` | A ByteString with content read from the given stream. |

**Remarks** If successful, `stream` will be read completely, from the position
at the start of the call.

### FromStreamAsync(Stream, CancellationToken)

    public static Task<ByteString> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default)

Constructs a [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from data in the given stream, asynchronously.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `stream` | `https://learn.microsoft.com/dotnet/api/system.io.stream` The stream to copy into a ByteString. |
| `cancellationToken` | `https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken` The cancellation token to use when reading from the stream, if any. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` | A ByteString with content read from the given stream. |

**Remarks** If successful, `stream` will be read completely, from the position
at the start of the call.

### GetEnumerator()

    public IEnumerator<byte> GetEnumerator()

Returns an iterator over the bytes in this [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerator-1https://learn.microsoft.com/dotnet/api/system.byte` | An iterator over the bytes in this object. |

### GetHashCode()

    public override int GetHashCode()

Returns a hash code for this object. Two equal byte strings
will return the same hash code.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` | A hash code for this object. |

**Overrides** [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode)

### ToBase64()

    public string ToBase64()

Converts this [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) into a standard base64 representation.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` | A base64 representation of this `ByteString`. |

### ToByteArray()

    public byte[] ToByteArray()

Converts this [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) into a byte array.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.byte` | A byte array with the same data as this `ByteString`. |

**Remarks** The data is copied - changes to the returned array will not be reflected in this `ByteString`.

### ToString(Encoding)

    public string ToString(Encoding encoding)

Converts this [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) into a string by applying the given encoding.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `encoding` | `https://learn.microsoft.com/dotnet/api/system.text.encoding` The encoding to use to decode the binary data into text. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` | The result of decoding the binary data with the given decoding. |

**Remarks** This method should only be used to convert binary data which was the result of encoding
text with the given encoding.

### ToStringUtf8()

    public string ToStringUtf8()

Converts this [ByteString](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) into a string by applying the UTF-8 encoding.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` | The result of decoding the binary data with the given decoding. |

**Remarks** This method should only be used to convert binary data which was the result of encoding
text with UTF-8.

### WriteTo(Stream)

    public void WriteTo(Stream outputStream)

Writes the entire byte array to the provided stream

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `outputStream` | `https://learn.microsoft.com/dotnet/api/system.io.stream` |

## Operators

### operator ==(ByteString, ByteString)

    public static bool operator ==(ByteString lhs, ByteString rhs)

Compares two byte strings for equality.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `lhs` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` The first byte string to compare. |
| `rhs` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` The second byte string to compare. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` | `true` if the byte strings are equal; false otherwise. |

### operator !=(ByteString, ByteString)

    public static bool operator !=(ByteString lhs, ByteString rhs)

Compares two byte strings for inequality.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `lhs` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` The first byte string to compare. |
| `rhs` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString` The second byte string to compare. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` | `false` if the byte strings are equal; true otherwise. |