Skip to content

Latest commit

 

History

History
79 lines (65 loc) · 1.66 KB

EXAMPLES_GenericExtensions.md

File metadata and controls

79 lines (65 loc) · 1.66 KB

Back to Package Contents

AsSingleElementSequence()

Suppose you have a method that expects an instance of IEnumerable<int> type but you have only a single int variable.

int x = 42;

void Process(IEnumerable<int> numbers)
{
}

Instead of

Process(new int[] { x });

you can write

Process(x.AsSingleElementSequence());

IsIn()

Suppose you want to check if a value matches only some specific members of an Enum type.

Instead of

bool IsWeekend(DateTime date)
{
    return
        date.DayOfWeek == DateOfWeek.Saturday ||
        date.DayOfWeek == DateOfWeek.Sunday;
}

you can write

using EgonsoftHU.Extensions.Bcl;

bool IsWeekend(DateTime date)
{
    return date.DayOfWeek.IsIn(DateOfWeek.Saturday, DateOfWeek.Sunday);
}

Suppose you want to check if a string value matches some specific values regardless of their casing.

Instead of

const string Development = nameof(Development);
const string Staging = nameof(Staging);

bool IsNonProductionEnvironment(string environment)
{
    environment = environment?.ToLower();

    return
        Development.ToLower() == environment ||
        Staging.ToLower() == environment;
}

you can write

using EgonsoftHU.Extensions.Bcl;

const string Development = nameof(Development);
const string Staging = nameof(Staging);

bool IsNonProductionEnvironment(string environment)
{
    return
        environment.IsIn(
            StringComparer.OrdinalIgnoreCase,
            Development,
            Staging
        );
}