ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

ToEnumerable

Convert an type T to IEnumerable<T>.

Source

public static class ObjectHelper
{
    public static IEnumerable<T> ToEnumerable<T>(this T input)
    {
        yield return input;
    }
}

Example

public static class ObjectHelper
{
    public static IEnumerable<T> ToEnumerable<T>(this T input)
    {
        yield return input;
    }
}

public class Employee
{
    public string Name { get; set; }
    public string Department { get; set; }
    public string Function { get; set; }
    public decimal Salary { get; set; }
}

public static class Program
{
    public static void Main()
    {
        var l = new List<Employee>();

        l.AddRange(Validator());

        Console.WriteLine();
        Console.Read();
    }

    public static IEnumerable<Employee> Validator()
    {
        return new Employee { Name = "Fons", Department = "R&D", Function = "Trainer", Salary = 2000 }
        .ToEnumerable();
    }
}

Author: Duong Nguyen

Submitted on: 31 dec 2019

Language: csharp

Type: System.Collections.Generic

Views: 3664