Why is it okay to call the SomeMethod() method in my example below?
I would think that the constructor parameter loggerFactory would no longer be available when the Logger property tries to access it.
I'm using a function when I set the lazyLogger field, but I thought it would throw an exception of sorts when I call the Logger property.
But everything works just fine. This might just be my misunderstanding of how the CLR/C# works.
I would appreciate an explanation for why it works this way.
public class TestClass
{
  private readonly Lazy<ILogger> lazyLogger;
  private ILogger Logger => this.lazyLogger.Value;
  public TestClass(ILoggerFactory loggerFactory)
  {
    this.lazyLogger = new Lazy<ILogger>(() = > loggerFactory.GetLogger("TestLogger"));
  }
  public void SomeMethod()
  {
    this.Logger.Info("Test Log Message"); //Why is it okay to call this method?  The constructor parameter shouldn't be available anymore, right?
  } 
}
public interface ILoggerFactory
{
  ILogger GetLogger(string name);
}
public interface ILogger
{
  void Info(string message);
}
public class TestLoggerFactory : ILoggerFactory
{
  public ILogger GetLogger(string name)
  {
      return new TestLogger(name);
  }
}
public class TestLogger : ILogger
{
  public void Info(string message)
  {
    Console.WriteLine(message);
  }
}
 
    