using System; public class Program { static void Main(string[] args) { var table = new ConditionalWeakTable(); table.Add(1, "Hello"); table.Add(2, "World"); int key = 2; if (table.TryGetValue(key, out string message)) Console.WriteLine($"The message for key {key} is '{message}'"); } }
using System; public class Person { public string Name { get; set; } public int Age { get; set; } } public class Program { static void Main(string[] args) { var table = new ConditionalWeakTableBoth examples use the System.Runtime.CompilerServices package library.(); var person1 = new Person { Name = "John Doe", Age = 30 }; var person2 = new Person { Name = "Jane Doe", Age = 25 }; table.Add("ID1", person1); table.Add("ID2", person2); string id = "ID1"; var person = table.GetValue(id, (k) => new Person()); Console.WriteLine($"The name of the person with ID {id} is {person.Name}"); } }