Computer
Computer

Reputation: 2227

Is it possible to return null from List<DateTime>?

I have an Interface and Class

Interface

List<DateTime> GetDates();

Class that implements the above Interface

public  List<DateTime> GetDates()
{
    IEnumerable<DateTime> MyDates = GetAllMyDates();
    IEnumerable<DateTime> ModifiedDates = null
    if (MyDates != null && MyDates.Count() > 0)
    {
        AllDates = MyDates.Where(....Some filter)
    }
    return AllDates.ToList();
}

Added a ? to the Interface but got the error

The type 'List' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable'

Searched on that error but couldnt figure out what ive done wrong or if its not possible to do this?

Upvotes: 1

Views: 156

Answers (1)

Dmitry
Dmitry

Reputation: 14059

Since the List<T> is a reference type you can just return null:

public List<DateTime> GetDates()
{
    IEnumerable<DateTime> MyDates = GetAllMyDates();
    IEnumerable<DateTime> ModifiedDates = null;

    if (MyDates != null && MyDates.Count() > 0)
    {
        AllDates = MyDates.Where(....Some filter)
        return AllDates.ToList();
    }

    return null;
}

Upvotes: 2

Related Questions