The short and simple version is this: I have a database. I have a c# list that I use to interact with that database. Program starts, dump the data into the list, program ends, save it back into the database.
Here’s my problem though: I need to make sure that one of my attributes in the list (id) is always unique when I create and remove list items. I was thinking maybe a list might have something similiar to a SQL auto-increment? Or perhaps just a numerical index of which item is in which location?
AddedDependents.Add(new Dependent
{
IsSpouse = isSpouse,
Id = /*unique id/*
});
You could just use the index of the list:
Then the
DependentId and list index are the same so it becomes trivial to get thatDependentfrom the list.As mentioned in the comments, if you are likely to delete elements from this list then you can’t just get the dependent via the Id, like
AddedDependents[dependent.Id]but instead would need to iterate through the list: