Looking at what you are doing with anList, we can infer that each element in anList is
not a simple Type, but an Object of some type, because you are accessing a Property of each item in anList: ".name" ...
To avoid for-loop, or forall iteration, over anList, you are going to have to use a different data structure.
There are many data structures you could consider, but I would be making my choice based on
what you know you are most probably going to be doing most frequently with whatever your objects contained in anList are, and their internal structure.
Internal structure: for example, could there be multiple instances of your objects whose .name Property value is the same: or, is each .name Property value unique ?
If all .name Property values are
unique strings: you could use a Dictionary[string, anList], where the string corresponds to what is now the .name property of each object in anList.
However, if many of your objects contain a .name Property that is identical, and you need to perform an operation on, or pull-out a collection of, all matches: that's another story.
Consider this small fragment of code from a WinForms project, where four CheckBoxes are put on a Form, and they all have their default unique names:
Dictionary<string, CheckBox> cbDict = new Dictionary<string, CheckBox>(
);
cbDict.Add(checkBox1.Name, checkBox1);
cbDict.Add(checkBox2.Name, checkBox2);
cbDict.Add(checkBox3.Name, checkBox3);
cbDict.Add(checkBox4.Name, checkBox4);
CheckBox nameMatchCheckBox;
if (cbDict.Keys.Contains("checkBox3"))
{
nameMatchCheckBox = cbDict["checkBox3"];
}
if (nameMatchCheckBox != null)
{
};
Note: I've omitted possible use of Linq here, which, perhaps, could lead to a simpler solution. This also might be a good opportunity to create an extension ?