Reputation: 39078
I've created the following interface.
public interface IMineralService
{
Task<Mineral[]> GetMinerals(int page = 0, int size = Int32.MaxValue);
Task<Mineral> GetMineral(int id);
}
Its implementation is as follows.
public class MineralService : IMineralService
{
public async Task<Mineral[]> GetMinerals(int page = 0, int size = Int32.MaxValue)
{
Mineral[] data = await Task.FromResult(Minerals);
return data.Skip(page * size).Take(size).ToArray();
}
public async Task<Mineral> GetMineral(int id)
{
Mineral[] data = await GetMinerals();
return data.SingleOrDefault(a => a.Id == id);
}
}
I don't understand what's the purpose or implication of declaring the default values for the parameters both in the constructor and in the implementation. Skipping the default values doesn't collide with implementing the interface as such, so the following would work. Id est - there's no imposition of the set defaults from the interface onto the implementation.
public interface IMineralService
{
Task<Mineral[]> GetMinerals(int page = 0, int size = Int32.MaxValue);
}
public class MineralService : IMineralService
{
public async Task<Mineral[]> GetMinerals(int page, int size) { ... }
}
It also works with the defaults only being declared in the implementation.
public interface IMineralService
{
Task<Mineral[]> GetMinerals(int page, int size);
}
public class MineralService : IMineralService
{
public async Task<Mineral[]> GetMinerals(int page = 0, int size = Int32.MaxValue)
}
However, in order to make the second method work with await GetMinerals(), the defaults must be set in the implementation. So what's the point of ever setting the default in the constructor if it must be re-stated in the implementation (if it's used by another call) or isn't needed at all (if there's no other call utilizing the defaults)?
Upvotes: 1
Views: 589
Reputation: 156504
Setting default values doesn't add any code to the class that the default values are set on, to set those values. Instead, it makes it so any consuming code that calls those methods without providing an explicit value will actually be compiled to pass the specified value.
Because of this, the optionality of these optional parameters is decoupled from the actual implementation details of the method.
That means it's possible for different interfaces that you implement to provide different default values. Or for the interfaces and the class to have different default values. Or for the implementing class to not even specify a default value at all, but have consumers of the interface still be able to call into that class's method without providing an explicit value.
So it's similar to deciding whether a class should implement an interface method explicitly versus implicitly: you need to think about how you expect your class and interface to be used.
Upvotes: 1