c# - Protobuf-Net Empty List -


came across protobuf-net, awesome! have question regarding serialization of empty lists.

i start declaring object want serialize:

[protocontract] class testclass {     [protomember(1)]     list<int> _listofints = new list<int>();      public testclass() { }      public list<int> listofints     {         { return _listofints; }         set { _listofints = value; }     } } 

if _listofints empty (but not null) when deserialse object null. makes sense looking @ protobuf convention , work around adding following method:

[protoafterdeserialization] private void ondeserialize() {     if (_listofints == null)         _listofints = new list<int>(); } 

my question whether can achieve same functionality in more concise fashion, possibly additional attirbute initialise null/empty objects empty instead of null?

if trying protect against null list try lazy loading in property getter.

public list<int> listofints {     { return _listofints ?? (_listofints = new list<int>()); }     set { _listofints = value; } } 

this way can allow serializer return null.


Comments