An encode/decode pair for a single field. This effectively encapsulates
all the information needed to read or write the field value from/to a coded
stream.
This class is public and has to be as it is used by generated code, but its public
API is very limited - just what the generated code needs to call directly.
This never writes default values to the stream, and does not address "packedness"
in repeated fields itself, other than to know whether or not the field should be packed.
Methods
CalculateSizeWithTag(T)
public int CalculateSizeWithTag(T value)
Calculates the size required to write the given value, with a tag,
if the value is not the default.
[[["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\u003eFieldCodec<T>\u003c/code\u003e is a class in the \u003ccode\u003eGoogle.Protobuf\u003c/code\u003e namespace that encapsulates the logic for encoding and decoding a single field's value to and from a coded stream.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eFieldCodec<T>\u003c/code\u003e class is primarily used by generated code and has a limited public API, focusing on the methods required by the generated code.\u003c/p\u003e\n"],["\u003cp\u003eIt handles the encoding/decoding of field values without writing default values, with special attention on if the field should be packed or not.\u003c/p\u003e\n"],["\u003cp\u003eThe class includes methods for reading (\u003ccode\u003eRead\u003c/code\u003e) and writing (\u003ccode\u003eWriteTagAndValue\u003c/code\u003e) field values to and from \u003ccode\u003eCodedInputStream\u003c/code\u003e, \u003ccode\u003eParseContext\u003c/code\u003e, and \u003ccode\u003eWriteContext\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThere is also a \u003ccode\u003eCalculateSizeWithTag\u003c/code\u003e method to calculate the size of the value with a tag.\u003c/p\u003e\n"]]],[],null,["# Class FieldCodec<T> (3.27.1)\n\nVersion latestkeyboard_arrow_down\n\n- [3.27.1 (latest)](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.FieldCodec-1)\n- [3.23.0](/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.FieldCodec-1)\n- [3.15.8](/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.FieldCodec-1) \n\n public sealed class FieldCodec\u003cT\u003e\n\n\nAn encode/decode pair for a single field. This effectively encapsulates\nall the information needed to read or write the field value from/to a coded\nstream.\n\n\nThis class is public and has to be as it is used by generated code, but its public\nAPI is very limited - just what the generated code needs to call directly. \n\nInheritance\n-----------\n\n[object](https://learn.microsoft.com/dotnet/api/system.object) \\\u003e FieldCodec\\\u003cT\\\u003e \n\nInherited Members\n-----------------\n\n[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) \n[object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) \n[object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) \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\nRemarks\n-------\n\nThis never writes default values to the stream, and does not address \"packedness\"\nin repeated fields itself, other than to know whether or not the field *should* be packed.\n\nMethods\n-------\n\n### CalculateSizeWithTag(T)\n\n public int CalculateSizeWithTag(T value)\n\nCalculates the size required to write the given value, with a tag,\nif the value is not the default.\n\n### Read(CodedInputStream)\n\n public T Read(CodedInputStream input)\n\nReads a value of the codec type from the given [CodedInputStream](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream).\n\n### Read(ref ParseContext)\n\n public T Read(ref ParseContext ctx)\n\nReads a value of the codec type from the given [ParseContext](/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ParseContext).\n\n### WriteTagAndValue(CodedOutputStream, T)\n\n public void WriteTagAndValue(CodedOutputStream output, T value)\n\nWrite a tag and the given value, *if* the value is not the default.\n\n### WriteTagAndValue(ref WriteContext, T)\n\n public void WriteTagAndValue(ref WriteContext ctx, T value)\n\nWrite a tag and the given value, *if* the value is not the default."]]