-1

I have a variable, Chunks, that is marked as { protected set; get; }. I want to mark this variable as NonSerialized so it won't be saved when I serialize the object that contains it.

However, I get the following error when I try this:

CS0592: Attribute 'NonSerialized' is not valid on this declaration type. It is valid on 'field' declarations only.

Here's the code I have:

[NonSerialized] public Chunk[,]? Chunks
{
    protected set;
    get;
}

Is Chunk not a field? If so, what is it and how can I replicate the NonSerialized property on my variable?

Rgamer43
  • 386
  • 2
  • 10
  • 2
    What you have is a [Property](https://learn.microsoft.com/en-us/dotnet/csharp/properties) not a [Field](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields). The attribute, [JsonIgnore](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/ignore-properties?pivots=dotnet-7-0) is probably what you are looking for, assuming you are using the `System.Text.Json` library. – quaabaam Sep 01 '23 at 23:36

0 Answers0