I have an Object called Graph.
I have list of Vertices in Graph class. So the Class would somewhat look like this. Its a .Net 2.0 code.
public class Graph
{
public List<Vertex> Vertices
{
get{ return _vertices}
}
} private List<Vertex> _vertices;
Here is my question. I would like to validate the Vertex object (by calling Vertex.IsValid() method) before adding it to the Graph object.
What’s the best way to do it? I like the way we add code inside the property’s getter block. Is there any similar way to it?
I have workarounds, but would like to do it in the best way possible.
The best solution is to not expose the list of vertices as a mutable list. Instead, if you need a
Verticesproperty, make it return a read-only data-structure. Then, just include anAdd()method, and validate all you want: