Add Extension Methods to Interfaces - CSharp Custom Type

CSharp examples for Custom Type:Extension Methods

Description

Add Extension Methods to Interfaces

Demo Code

using System;/*from ww  w .j  ava  2  s.co  m*/
using System.Collections.Generic;

public static class IEnumerableHelper
{

    public static T First<T>(this IEnumerable<T> sequence)
    {
        foreach (T element in sequence)
            return element;

        throw new InvalidOperationException("No elements!");
    }
}

class Test
{
    static void Main()
    {
        Console.WriteLine("Seattle".First());   // S
    }
}

Result


Related Tutorials