Is it possible to have an optional parameter for a constructor with dependency injection. I know that dependency injection usually it implies it is a required dependency, but for example if the only dependency is an
ILogger
ILogger
, then I don't really care if one was registered or not.
EDIT: Changed
ILogger<Test>
ILogger<Test>
to be nullable
public class Test{ private readonly ILogger<Test>? _logger; public Test(ILogger<Test>? logger = null) // does this work? { _logger = logger } public void DoSomething() { _logger?.LogInformation("Doing something..."); }}
public class Test{ private readonly ILogger<Test>? _logger; public Test(ILogger<Test>? logger = null) // does this work? { _logger = logger } public void DoSomething() { _logger?.LogInformation("Doing something..."); }}