Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Path
- {
- public List<Segment> Segments { get; } = new List<Segment>();
- }
- public class Segment
- {
- public int Property { get; }
- public Path Path { get; }
- ...
- }
- public static class Test
- {
- public static Path CreatePath()
- {
- var path = new Path();
- var segment = new Segment(path, 42);
- return path;
- }
- }
- public Segment(Path path, int property)
- {
- Property = property;
- Path = path;
- path.Segments.Add(this); // <--
- }
- namespace PathExample0
- {
- public class Path
- {
- public List<Segment> Segments { get; } = new List<Segment>();
- }
- public class Segment
- {
- public int Property { get; }
- public Path Path { get; }
- public Segment(Path path, int property)
- {
- Property = property;
- Path = path;
- path.Segments.Add(this);
- }
- }
- public static class Test
- {
- public static Path CreatePath()
- {
- var path = new Path();
- var segment = new Segment(path, 42);
- return path;
- }
- }
- }
- namespace PathExample1
- {
- public class Path
- {
- public List<Segment> Segments { get; } = new List<Segment>();
- }
- public class Segment
- {
- public int Property { get; }
- public Path Path { get; }
- public Segment(Path path, int property)
- {
- Property = property;
- Path = path;
- }
- }
- public static class Test
- {
- public static Path CreatePath()
- {
- var path = new Path();
- var segment = new Segment(path, 42);
- path.Segments.Add(segment);
- return path;
- }
- }
- }
- namespace PathExample2
- {
- public class Path
- {
- private readonly List<Segment> segments = new List<Segment>();
- public IReadOnlyList<Segment> Segments => segments;
- public void AddSegment(int property)
- {
- var segment = new Segment(this, property);
- segments.Add(segment);
- }
- }
- public class Segment
- {
- public int Property { get; }
- public Path Path { get; }
- public Segment(Path path, int property)
- {
- Property = property;
- Path = path;
- }
- }
- public static class Test
- {
- public static Path CreatePath()
- {
- var path = new Path();
- path.AddSegment(42);
- return path;
- }
- }
- }
- namespace PathExample3
- {
- public interface ISegment
- {
- Path Path { get; }
- int Property { get; }
- }
- public class Path
- {
- private readonly List<Segment> segments = new List<Segment>();
- public IReadOnlyList<ISegment> Segments => segments;
- public void AddSegment(int property)
- {
- var segment = new Segment(this, property);
- segments.Add(segment);
- }
- private class Segment : ISegment
- {
- public int Property { get; }
- public Path Path { get; }
- public Segment(Path path, int property)
- {
- Property = property;
- Path = path;
- }
- }
- }
- public static class Test
- {
- public static Path CreatePath()
- {
- var path = new Path();
- path.AddSegment(42);
- return path;
- }
- }
- }
- public class Segment
- {
- private Segment(Path path, int property)
- {
- Path = path;
- Property = property;
- }
- public int Property { get; }
- public Path Path { get; }
- public static Segment AddToPath(Path path, int property)
- {
- var segment = new Segment(path, property);
- path.Segments.Add(segment);
- return segment;
- }
- }
- public class Path
- {
- private readonly List<Segment> segments = new List<Segment>();
- public IReadOnlyList<Segment> Segments => segments;
- public Segment AddSegment(int property)
- {
- return Segment.AddToPath(this, property);
- }
- }
- public class Path
- {
- private readonly List<Segment> segments = new List<Segment>();
- public IReadOnlyList<Segment> Segments => segments;
- public void AddSegment(int property)
- {
- AddSegment(new Segment(property));
- }
- public void AddSegment(Segment segment)
- {
- if (segment.Path != null) throw ...;
- segment.Path = segment;
- segments.Add(segment);
- }
- }
- public class Segment
- {
- public int Property { get; }
- public Path Path { get; internal set; }
- public Segment(int property)
- {
- Property = property;
- }
- }
Add Comment
Please, Sign In to add comment