Create Custom LINQ Extension Methods - CSharp LINQ

CSharp examples for LINQ:IEnumerable

Description

Create Custom LINQ Extension Methods

Demo Code


using System;//  w w w  . j  ava2s  . c  o  m
using System.Collections.Generic;
using System.Linq;
using System.Text;

    static class LINQExtensions
    {
        public static IEnumerable<string> RemoveFirstAndLast(this IEnumerable<string> source)
        {
            return source.Skip(1).Take(source.Count() - 2);
        }
    }

class MainClass
    {
        static void Main(string[] args)
        {
            string[] ds1 = {"Oracle", "file", "pear", "fig"};
            IList<string> ds2 = new List<string> { "Oracle", "file", "pear", "fig" };

            Console.WriteLine("Extension method used on string[]");
            IEnumerable<string> result1 = ds1.RemoveFirstAndLast();
            foreach (string element in result1)
            {
                Console.WriteLine("Result: {0}", element);
            }

            Console.WriteLine("\nExtension method used on IList<string>");
            IEnumerable<string> result2 = ds1.RemoveFirstAndLast();
            foreach (string element in result2)
            {
                Console.WriteLine("Result: {0}", element);
            }
        }
    }

Result


Related Tutorials