I have a class that has 4 properties, one of which is a string.
I make a collection of these class objects like so:
List<MyObject> myObjects = new List<MyObject>();
I then start throwing new objects in the collection:
My question is: how can I find out if there is already an object in the collection that has one of the properties at a specific value so I don't add it again?
For example if my next object had a name of "yatta" I would not want to add it again. But how can I find out if it is already in there?
I know that I can do the following but it seems clunky:
There has to be a better way that I can do this where the code for it would be in my class and not in my program code.
I make a collection of these class objects like so:
List<MyObject> myObjects = new List<MyObject>();
I then start throwing new objects in the collection:
Code:
MyObject m = new MyObject();
m.name = "yatta";
m.id = 12;
m.isBig = true;
m.dbl = 12.5;
myObjects.Add(m);
My question is: how can I find out if there is already an object in the collection that has one of the properties at a specific value so I don't add it again?
For example if my next object had a name of "yatta" I would not want to add it again. But how can I find out if it is already in there?
I know that I can do the following but it seems clunky:
Code:
bool foundOne = false;
forech (MyObject i in myObjects)
{
if (i.name = m.name)
{
foundOne = true;
}
}
There has to be a better way that I can do this where the code for it would be in my class and not in my program code.