C# private, static, and readonly

前端 未结 9 742
忘掉有多难
忘掉有多难 2020-12-25 12:27

I was reviewing some code for log4net and I came across this.

private static readonly ILog logger = LogManager.GetLogger(typeof(AdminClient));
9条回答
  •  既然无缘
    2020-12-25 13:04

    I think you're misunderstanding static. Static doesn't mean "can't be used outside the file." Static means: there's one per class. What this declaration does is creates a logger that is only allocated once (static), only available in the class (not in derived classes either) (private), and cannot be written to past its initialization (readonly).

    Good question though!

提交回复
热议问题