title: References sidebar_position: 6 id: references license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Apache Fory™ C# can preserve shared and circular references when TrackRef(true) is enabled.
Fory fory = Fory.Builder() .TrackRef(true) .Build();
When enabled:
using Apache.Fory; [ForyObject] public sealed class Node { public int Value { get; set; } public Node? Next { get; set; } } Fory fory = Fory.Builder() .TrackRef(true) .Build(); fory.Register<Node>(200); Node node = new() { Value = 7 }; node.Next = node; byte[] payload = fory.Serialize(node); Node decoded = fory.Deserialize<Node>(payload); // The cycle is preserved. System.Diagnostics.Debug.Assert(object.ReferenceEquals(decoded, decoded.Next));
TrackRef(false)TrackRef(false) can be faster for tree-like, acyclic data where reference identity does not matter.