c# - List method creation -
i'm creating list of defined objects so
list<clock> cclocks = new list<clocks>();
for each object in list i'm calling method movetime, so
foreach(clock c in cclocks) { c.movetime(); }
is way can write cleaver thing can call
cclocks.movetime();
it go though list doing method
i guess want create collection method?
i'm guessing there must thing can don't know what.
thanks help
you write extension method on list<t>
iterates this
, calls movetime()
on each of items in collection. see this article more information.
this approach obscures lot of information, though. if we're you, i'd go for-loop. , if you're calling 1 method on each of objects, can shorten for-loop, so:
// no need declare scope if you're doing 1 operation on collection foreach(var object in collection) object.method();
... or use linq:
collection.foreach(object => object.method());
Comments
Post a Comment