I have the following C# code:
List<string> names = new List<string>();
if (myObject.myClass != null)
{
foreach (var foo in myObject.myClass.subClass)
{
names.Add(foo.Name);
}
}
Is there a shorter way of writing this without the need to iterate through each instance of subClass to add myObject.myClass.subClass.Name into the names List<string> object?
Also baring in mind myObject.myClass can be null.
It sounds like you could use:
Note that this expression:
myObject.myClass.subclasssuggests you’re violating the Law of Demeter fairly nastily – and I hope the naming doesn’t actually reflect reality though… if you could provide a more indicative set of expressions, that would help a lot.