Using the ForEach () method of a generic list:
static void Main ()
{
\u0026lt;String> List names = new \u0026lt;String> List ();
names.Add( "Bruce" );
names.Add( "Alfred" );
names.Add( "Tim" );
names.Add( "Richard" );
// Display the contents of the list using the Print method.
names.ForEach(Print);
// The following demonstrates the anonymous method feature of C#
// to display the contents of the list to the console.
names.ForEach( delegate (String name)
{
Console.WriteLine(name);
});
} private static void Print (string s)
{Console.WriteLine (s);}
Generic as the extension method for IEnumerable
public static class IEnumerableExtension
{public static void PrintIt \u0026lt;T> ( this T item) {
Console.WriteLine (item.ToString ());}
public static void PrintAllItems \u0026lt;T> ( this \u0026lt;T> IEnumerable enumerable) {
foreach (T item in enumerable) {
PrintIt (item);}
}}
0 comments:
Post a Comment