'Check if input is empty for each method

I have a class which has a list constructor input and has a lot of functions returning doubles. An empty list is a valid input in some cases but almost all functions return _isEmpty ? double.NaN : _myList.Something.

Is it possible to optimize the repeating check?

public class test
{
private readonly List<double> _myList;
private readonly bool _isEmpty;

public test(List<double> mylist){
_myList = myList
_isEmpty = myList.Count == 0;
}

public double GetFirst()
{
return _isEmpty ? double.NaN : _myList.First()
}

public double GetLast()
{
return _isEmpty ? double.NaN : _myList.Last()
}

public double GetSum()
{
return _isEmpty ? double.NaN : _myList.Sum()
}

...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source