# Class FieldCodec&lt;T&gt; (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.FieldCodec-1)
- [3.23.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.FieldCodec-1)
- [3.15.8](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.FieldCodec-1)

    public sealed class FieldCodec<T>


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.

## Inheritance

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

## Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [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

| ## Type Parameter ||
|---|---|
| **Name** | **Description** |
| `T` |   |

## Remarks

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.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `value` | `T` |

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

### Read(CodedInputStream)

    public T Read(CodedInputStream input)

Reads a value of the codec type from the given [CodedInputStream](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `input` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream` The input stream to read from. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The value read from the stream. |

### Read(ref ParseContext)

    public T Read(ref ParseContext ctx)

Reads a value of the codec type from the given [ParseContext](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ParseContext).

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `ctx` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ParseContext` The parse context to read from. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `T` | The value read. |

### WriteTagAndValue(CodedOutputStream, T)

    public void WriteTagAndValue(CodedOutputStream output, T value)

Write a tag and the given value, *if* the value is not the default.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `output` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedOutputStream` |
| `value` | `T` |

### WriteTagAndValue(ref WriteContext, T)

    public void WriteTagAndValue(ref WriteContext ctx, T value)

Write a tag and the given value, *if* the value is not the default.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `ctx` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WriteContext` |
| `value` | `T` |