I am trying to understand some code we went over today in class where we created a class below,
class Datapoint
{
private string latitude;
private string longtitude;
private string elevation;
public Datapoint()
{
latitude = "no data";
longtitude = "no data";
elevation = "no data";
}
public Datapoint(string latitude, string longtitude, string elevation)
{
this.latitude = latitude;
this.longtitude = longtitude;
this.elevation = elevation;
}
public string Latitude { get { return latitude; } }
public string Longtitude { get { return longtitude; } }
public string Elevation { get { return elevation; } }
}
and then made a list from this class,
List<Datapoint> datapoints;
Is this a list that goes
latitude
longtitude
elevation
latitude
longtitude
elevation
latitude
longtitude
elevation
latitude
longtitude
elevation
and so on and so forth? Thank you for any help.
It seems you are thinking of the
Datapointclass as itself a type of list structure. While you could think of it this way, that isn’t the way most programmers would visualize it.An easier entity to understand might be a person. Do you think of a person as a list, like this?
…and so on? I think not. A person has many attributes or properties; but if you consider a list of people, this list is not simply a concatenated sequence of person-properties but rather of person entities (or instances).
So as others have pointed out, a
List<Datapoint>, or a list ofDatapointobjects, is not merely a list of the properties of these objects but in fact a list of the objects themselves (each of which has properties).At a lower level, if you’re asking how this information is stored then you’re actually close. Obviously, for a
Datapointobject to be allocated in memory, there must be room for all of its properties; and these will most likely be allocated in a contiguous block (though at this point we’re getting to a level of implementation detail most developers would argue you shouldn’t even be thinking about for purposes of writing software—that’s another discussion I’ll save for someone else!).Now, you might think that it would therefore make sense to store a list of
Datapointobjects much like you suggested:However, as you may have learned in class already, reference types are not stored in this way in .NET. Given a collection of objects, each instance may be stored anywhere; what you have in a
List<Datapoint>is actually a bunch of references to these objects, in much the same way that if I had this list:…the buildings at these addresses would not need to actually reside next to one another.
Hopefully this helps you to understand what a
List<Datapoint>actually is: a list of references to objects, which have properties but are not traditionally thought of as lists themselves (though in some sense they are), and which may be allocated and exist at non-adjacent points in memory.