I have a repository class that I want to add two methods to:
public IEnumerable<OpenCall> OpenCalls()
{
return something;
}
public IEnumerable<OpenCall> OpenCalls(DateTime start, DateTime endd)
{
return something_slightly_different;
}
Inside each method I am going to call another method (AverageResolutions()) that returns a list of average resolutions. Obviously this method will also need to take 0 parameters or 2 parameters. The way I’m doing things at the moment I’m either going to end with two almost identical copies of OpenCalls(), or two almost identical copies of AverageResolutions(), altered slightly to allow for the DateTime parameters.
I think I’m doing this wrong – how can I just end up with one version of a method that will either take 0 or 2 parameters and then decide what to call further down the line if they are either null or not null?
You could change the method signature to use optional arguments:
Note that you will need to make the
DateTime-parameters nullable, because otherwise you wouldn’t be able to set default values (which have to be compile-time constants).For more information about nullable types, and the syntactic sugar (writing
DateTime?instead ofNullable<DateTime>see the MSDN article on Nullable TypesFor more information about optional parameters see the Optional Arguments section of the MSDN article about Named and Optional Arguments.