In C#, instance methods can only be called on an instance, whereas static methods can only be called on a class/struct itself.
Why can't you chain Info
onto SimpleLogger.Instance()
?
Because SimpleLogger.Instance(...)
returns an instance of SimpleLogger
, and you are trying to call a static method on the returned value. The returned value is an instance of SimpleLogger
, so you can't call a static method on it.
By making Info
non-static, you enable it to be called on an instance. Therefore, you can call it on the return value of Instance()
.
One reason for your confusion might be that you don't see the instance of SimpleLogger
in your chain of methods, so to better illustrate the idea of chaining methods, this:
SimpleLogger.Instance("path").Info("info");
is equivalent to:
SimpleLogger logger = impleLogger.Instance("path");
logger.Info("info");
See the instance of SimpleLogger
now?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…