Skip to content

Commit 5cc81fc

Browse files
committed
Update README
1 parent 195dc58 commit 5cc81fc

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public partial class Person3
213213
When serializing/deserializing, MemoryPack can invoke a before/after event using the `[MemoryPackOnSerializing]`, `[MemoryPackOnSerialized]`, `[MemoryPackOnDeserializing]`, `[MemoryPackOnDeserialized]` attributes. It can annotate both static and instance (non-static) methods, and public and private methods.
214214

215215
```csharp
216-
[MemoryPackable]
216+
[MemoryPackable]
217217
public partial class MethodCallSample
218218
{
219219
// method call order is static -> instance
@@ -546,6 +546,28 @@ public partial class VersionCheck
546546

547547
In use-case, store old data (to file, to redis, etc...) and read to new schema is always ok. In the RPC scenario, schema exists both on the client and the server side, the client must be updated before the server. An updated client has no problem connecting to the old server but an old client can not connect to a new server.
548548

549+
550+
By default, when the old data read to new schema, any members not on the data side are initialized with the `default` literal.
551+
If you want to avoid this and use initial values of field/properties, you can use `[SuppressDefaultInitialization]`.
552+
553+
```cs
554+
[MemoryPackable]
555+
public partial class DefaultValue
556+
{
557+
public string Prop1 { get; set; }
558+
559+
[SuppressDefaultInitialization]
560+
public int Prop2 { get; set; } = 111; // < if old data is missing, set `111`.
561+
562+
public int Prop3 { get; set; } = 222; // < if old data is missing, set `default`.
563+
}
564+
```
565+
566+
`[SuppressDefaultInitialization]` has following disadvantages:
567+
- Cannot be used with readonly, init-only, and required modifier.
568+
=- May not be the best performance due to increased conditional branching. (But it would be negligible.)
569+
570+
549571
The next [Serialization info](#serialization-info) section shows how to check for schema changes, e.g., by CI, to prevent accidents.
550572

551573
When using `GenerateType.VersionTolerant`, it supports full version-tolerant.

0 commit comments

Comments
 (0)