Skip to content

Instantly share code, notes, and snippets.

@slavanap
Last active September 26, 2020 22:37
Show Gist options
  • Save slavanap/c4adf595d1ab2dadb97be3edf8a9fb07 to your computer and use it in GitHub Desktop.
Save slavanap/c4adf595d1ab2dadb97be3edf8a9fb07 to your computer and use it in GitHub Desktop.
Box value to modify it in-place in dictionary
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
namespace ConsoleApp3 {
public class Box<T> {
public T Value;
public Box(T value) {
Value = value;
}
public override string ToString() => Value.ToString();
public override int GetHashCode() => Value.GetHashCode();
public override bool Equals(object obj) {
if (obj is Box<T> box)
return base.Equals(box) || Value.Equals(box.Value);
return Value.Equals(obj);
}
}
public static class Box {
public static Box<T> Create<T>(T value) => new Box<T>(value);
}
class Program {
static void Main(string[] args) {
var x = JObject.Parse(@"{ ""x"": [1 ] }");
Console.WriteLine(x["x"].GetType());
x["z"] = new JObject();
x["z"]["b"] = 100500;
JObject y = new JObject();
y["z"] = x["z"];
x["b"] = y = (JObject)x["z"];
y["b"] = 42;
Console.WriteLine(x.ToString(Newtonsoft.Json.Formatting.Indented));
Console.WriteLine(y.ToString(Newtonsoft.Json.Formatting.Indented));
var a = new Dictionary<int, Box<int>>();
a[1] = Box.Create(2);
foreach (var kv in a) {
kv.Value.Value = 4;
}
Console.WriteLine(a[1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment