I am writing a Graph library in C#.
Here is my Graph.cs :
public class Graph : IGraph
{
public HashSet<INode> Nodes { get; set; }
public HashSet<IEdge> Edges { get; set; }
}
- Rule 1 : When a Node is deleted, I want to all edges that deleted node was part of them , become deleted from HashSet of IEdge.
- Rule 2 : When an Edge is added, I want to both Node instances in Edge instanse add to HashSet of INode.
What can I do for gaining this behavior?
Now, the library user simply use :
g.Edges.Add(new Edge(n5, n6));
that n5 and n6 are Node
instanses but there is no n5 and n6 in g.Nodes HashSet.
I was wondering if there is a way that I could call a method like this when an Edge instanse is added to HashSet in the Setter of Edges property :
void UpdateNodes(IEdge edge)
{
Nodes.Add(edge.A_Node);
Nodes.Add(edge.Another_Node);
}