CerIs
CerIs

Reputation: 567

Possible to serialize as T and deserialize as List<T>?

I can deserialize all items in my binary file only if I add it as a collection (List< T > in this case).

Makes sense on one level however I am only ever appending one item to the list, so I`m creating a List with one item in it then serializing, only then can I deserialize as List< T >. Is there a performance hit for doing this or am I overthinking it?

Upvotes: 1

Views: 59

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062945

If you are only ever appending one item to the list, then yes there is some marginal overhead in having the list, but interestingly this only impacts runtime - the actual serialization of a list with one item vs a direct (non-list) sub-property is identical. So; if you are absolutely sure you only have (at most) one item, the following will be interchangeable:

[ProtoMember(42)]
public List<Foo> Foos {get;} = new List<Foo>(); // has at most one item

and

[ProtoMember(42)]
public Foo Foo {get;set;}

Upvotes: 1

Related Questions