syntax - C# is there a foreach oneliner available? -
i want know if there foreach oneliner in c#, if oneliner (exp) ? then : else
.
if you're dealing array can use built-in static foreach
method:
array.foreach(yourarray, x => console.writeline(x));
if you're dealing list<t>
can use built-in foreach
instance method:
yourlist.foreach(x => console.writeline(x));
there's nothing built-in that'll work against arbitrary ienumerable<t>
sequence, it's easy enough roll own extension method if feel need it:
yoursequence.foreach(x => console.writeline(x)); // ... public static class enumerableextensions { public static void foreach<t>(this ienumerable<t> source, action<t> action) { if (source == null) throw new argumentnullexception("source"); if (action == null) throw new argumentnullexception("action"); foreach (t item in source) { action(item); } } }
Comments
Post a Comment