C# private, static, and readonly

前端 未结 9 734
忘掉有多难
忘掉有多难 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!

    0 讨论(0)
  • 2020-12-25 13:07

    static does not mean that it cannot be accessed from other files - this isn't C. The static keyword means that the logger object is a class variable instead of an instance variable, so even when accessed from different objects of that class, they will all refer to the same logger object.

    0 讨论(0)
  • 2020-12-25 13:09

    static in c# means the member is associated with the class, and not with an instance of the class. Readonly is important because in c# most variables, and this one in particular, are reference variables. The readonly means that this variable will always reference the same logger.

    0 讨论(0)
  • 2020-12-25 13:09

    A readonly variable is very much like const in that the value is constant throughout its lifetime. The difference is that a readonly variable is initialized at run-time and const is at compile time. Static, in sort of laymen terms, means that the instance of the variable does not depend on the instance of the object it is declared in. Its lifetime persists from function call to function call. A static variable is faster to access because its storage remains allocated for the entire duration of the program. So knowing this we can go back to your question.

    Why is 'logger' a static member? That's a design decision. I need to know how you're using it to answer this question. Why is it readonly? Because it seems like it's initialized once and its instance is used throughout. We can make sure that nobody else tampers with the value of logger by making it 'read-only' right after we initialize it.

    0 讨论(0)
  • 2020-12-25 13:12
    • private No one should use the logger field outside the class (even in subclasses), if you don't set this any other class could use your logger for log in your class' name.
    • static The attribute is attached to the class so it won't repeat with each instance of the class. If you don't set this the logger attribute will occupy extra space in memory with every instance the system makes of the object (you misunderstood this).
    • readonly The logger field shouldn't be modified.
    0 讨论(0)
  • 2020-12-25 13:13

    What the developer is saying is that when they call logger.Info(...) in any instance of this class they want to use a common (static) instance (so don't need to create a new logger per class instance), they want to be certain that it hasn't changed since it was created (readonly) and if we are in virtual function in a derived class then I want to make sure I don't use the base class one by mistake (private).

    0 讨论(0)
提交回复
热议问题