public static ByteString CopyFrom(params byte[] bytes)
Constructs a 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.
public static ByteString CopyFrom(ReadOnlySpan<byte> bytes)
Constructs a ByteString from a read only span. The contents
are copied, so further modifications to the span will not
be reflected in the returned ByteString.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003e\u003ccode\u003eByteString\u003c/code\u003e is an immutable array of bytes within the Google.Protobuf namespace, implementing interfaces for byte enumeration and equality comparison.\u003c/p\u003e\n"],["\u003cp\u003eIt offers properties like \u003ccode\u003eEmpty\u003c/code\u003e, \u003ccode\u003eIsEmpty\u003c/code\u003e, \u003ccode\u003eLength\u003c/code\u003e, \u003ccode\u003eMemory\u003c/code\u003e, and \u003ccode\u003eSpan\u003c/code\u003e to access and manage the byte data, along with an indexer for individual byte retrieval.\u003c/p\u003e\n"],["\u003cp\u003eVarious static methods are available to construct a \u003ccode\u003eByteString\u003c/code\u003e from byte arrays, read-only spans, strings, Base64 encoded strings, or streams, including both synchronous and asynchronous stream operations.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eByteString\u003c/code\u003e class can be converted to other formats, such as byte arrays, Base64 strings, and UTF-8 encoded strings, and also allows comparison for equality or inequality with other \u003ccode\u003eByteString\u003c/code\u003e instances.\u003c/p\u003e\n"],["\u003cp\u003eMethods are provided for copying data to byte arrays, creating a \u003ccode\u003eCodedInputStream\u003c/code\u003e, obtaining iterators, calculating hash codes, writing to a stream, and encoding the data into strings using a given encoding or UTF-8.\u003c/p\u003e\n"]]],[],null,["# Class ByteString (3.27.1)\n\nVersion latestkeyboard_arrow_down\n\n- [3.27.1 (latest)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString)\n- [3.23.0](/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.ByteString)\n- [3.15.8](/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.ByteString) \n\n public sealed class ByteString : IEnumerable\u003cbyte\u003e, IEnumerable, IEquatable\u003cByteString\u003e\n\nImmutable array of bytes. \n\nInheritance\n-----------\n\n[object](https://learn.microsoft.com/dotnet/api/system.object) \\\u003e ByteString \n\nImplements\n----------\n\n[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](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) \n\nInherited Members\n-----------------\n\n[object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) \n[object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) \n[object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) \n[object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)\n\nNamespace\n---------\n\n[Google.Protobuf](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf)\n\nAssembly\n--------\n\nGoogle.Protobuf.dll\n\nProperties\n----------\n\n### Empty\n\n public static ByteString Empty { get; }\n\nReturns an empty ByteString.\n\n### IsEmpty\n\n public bool IsEmpty { get; }\n\nReturns `true` if this byte string is empty, `false` otherwise.\n\n### this\\[int\\]\n\n public byte this[int index] { get; }\n\nReturns the byte at the given index.\n\n### Length\n\n public int Length { get; }\n\nReturns the length of this ByteString in bytes.\n\n### Memory\n\n public ReadOnlyMemory\u003cbyte\u003e Memory { get; }\n\nProvides read-only access to the data of this [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString).\nNo data is copied so this is the most efficient way of accessing.\n\n### Span\n\n public ReadOnlySpan\u003cbyte\u003e Span { get; }\n\nProvides read-only access to the data of this [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString).\nNo data is copied so this is the most efficient way of accessing.\n\nMethods\n-------\n\n### CopyFrom(params byte\\[\\])\n\n public static ByteString CopyFrom(params byte[] bytes)\n\nConstructs a [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from the given array. The contents\nare copied, so further modifications to the array will not\nbe reflected in the returned ByteString.\nThis method can also be invoked in `ByteString.CopyFrom(0xaa, 0xbb, ...)` form\nwhich is primarily useful for testing.\n\n### CopyFrom(byte\\[\\], int, int)\n\n public static ByteString CopyFrom(byte[] bytes, int offset, int count)\n\nConstructs a [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from a portion of a byte array.\n\n### CopyFrom(ReadOnlySpan\\\u003cbyte\\\u003e)\n\n public static ByteString CopyFrom(ReadOnlySpan\u003cbyte\u003e bytes)\n\nConstructs a [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from a read only span. The contents\nare copied, so further modifications to the span will not\nbe reflected in the returned [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString).\n\n### CopyFrom(string, Encoding)\n\n public static ByteString CopyFrom(string text, Encoding encoding)\n\nCreates a new [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) by encoding the specified text with\nthe given encoding.\n\n### CopyFromUtf8(string)\n\n public static ByteString CopyFromUtf8(string text)\n\nCreates a new [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) by encoding the specified text in UTF-8.\n\n### CopyTo(byte\\[\\], int)\n\n public void CopyTo(byte[] array, int position)\n\nCopies the entire byte array to the destination array provided at the offset specified.\n\n### CreateCodedInput()\n\n public CodedInputStream CreateCodedInput()\n\nCreates a CodedInputStream from this ByteString's data.\n\n### Equals(ByteString)\n\n public bool Equals(ByteString other)\n\nCompares this byte string with another.\n\n### Equals(object)\n\n public override bool Equals(object obj)\n\nCompares this byte string with another object.\n\n**Overrides** \n[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object))\n\n### FromBase64(string)\n\n public static ByteString FromBase64(string bytes)\n\nConstructs a [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from the Base64 Encoded String.\n\n### FromStream(Stream)\n\n public static ByteString FromStream(Stream stream)\n\nConstructs a [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from data in the given stream, synchronously.\n\n**Remarks** \nIf successful, `stream` will be read completely, from the position\nat the start of the call.\n\n### FromStreamAsync(Stream, CancellationToken)\n\n public static Task\u003cByteString\u003e FromStreamAsync(Stream stream, CancellationToken cancellationToken = default)\n\nConstructs a [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) from data in the given stream, asynchronously.\n\n**Remarks** \nIf successful, `stream` will be read completely, from the position\nat the start of the call.\n\n### GetEnumerator()\n\n public IEnumerator\u003cbyte\u003e GetEnumerator()\n\nReturns an iterator over the bytes in this [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString).\n\n### GetHashCode()\n\n public override int GetHashCode()\n\nReturns a hash code for this object. Two equal byte strings\nwill return the same hash code.\n\n**Overrides** \n[object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode)\n\n### ToBase64()\n\n public string ToBase64()\n\nConverts this [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) into a standard base64 representation.\n\n### ToByteArray()\n\n public byte[] ToByteArray()\n\nConverts this [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) into a byte array.\n\n**Remarks** \nThe data is copied - changes to the returned array will not be reflected in this `ByteString`.\n\n### ToString(Encoding)\n\n public string ToString(Encoding encoding)\n\nConverts this [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) into a string by applying the given encoding.\n\n**Remarks** \nThis method should only be used to convert binary data which was the result of encoding\ntext with the given encoding.\n\n### ToStringUtf8()\n\n public string ToStringUtf8()\n\nConverts this [ByteString](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ByteString) into a string by applying the UTF-8 encoding.\n\n**Remarks** \nThis method should only be used to convert binary data which was the result of encoding\ntext with UTF-8.\n\n### WriteTo(Stream)\n\n public void WriteTo(Stream outputStream)\n\nWrites the entire byte array to the provided stream\n\nOperators\n---------\n\n### operator ==(ByteString, ByteString)\n\n public static bool operator ==(ByteString lhs, ByteString rhs)\n\nCompares two byte strings for equality.\n\n### operator !=(ByteString, ByteString)\n\n public static bool operator !=(ByteString lhs, ByteString rhs)\n\nCompares two byte strings for inequality."]]