I have an example code like this:
public class SimpleLogger
{
private static SimpleLogger logger;
private string path = null;
protected SimpleLogger(string path)
{
this.path = path;
}
public static SimpleLogger Instance(string path)
{
if (logger == null)
{
logger = new SimpleLogger(path);
}
return logger;
}
public static void Info(string info)
{
string path = $"{logger.path}{DateTime.Now.ToShortDateString()}_Info.txt";
using (StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine($"{DateTime.Now} - {info}");
}
}
}
and when I call:
SimpleLogger.Instance("path").Info("info");
There's an error:
member cannot be accessed with an instance reference qualify it with a type name instead static method
But I DO use type name, don't I?
But when I call it like this:
SimpleLogger.Instance("path");
SimpleLogger.Info("info");
it actually does work fine.
To make it work inline I have to make Info method non-static and then inline call work also fine. Why is that? I don't understand the mechanism here. Can someone explain? Is it beacuse Instance method returns SimpleLogger object and then info requires to be non-static to be able to work on an instance rather than a type?