views:

1496

answers:

8

Hello,

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

private static readonly ILog logger = LogManager.GetLogger(typeof(AdminClient));

I am wondering why would you need to have private static readonly.

From my understanding private would mean that the variable cannot not be used outside the class unless there is a accessor method or get property.

static would mean that the variable is scoped only in this file only.

readonly would mean that you can only read from the value and cannot assign it.

So, I am thinking that the person who wrote this code. declared it private as they don't want it used outside the class and static so that don't want it used outside the file. However, if there is a get property would static prevent this form happening.

I think I can understand readonly and the value is only to be read from and not set.

Many thanks for any advice,

+21  A: 
  • 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, check out this link).
  • readonly The logger field shouldn't be modified.
victor hugo
nice, you left all very clear here
chermosillo
+4  A: 

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.

Tal Pressman
+7  A: 

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!

Stefan Mai
+2  A: 

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.

Robert
+1  A: 

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.

Sergey
+1  A: 

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).

Colin Desmond
+1  A: 

The reason to put a readonly flag on a private variable is to declare that the variable will always reference the same object. It's true that being private makes it invisible to anyone outside the class, but this way we can make sure we didn't accidentaly overwrite the variable with a new object, by writing something like

logger = LogManager.GetLogger(typeof(AdminClient));

somewhere else in our class. With readonly it just won't compile (Unless it was not initialized before, and we are in the (static) constructor)

Noam Gal
+1  A: 

Static variables falls in category of "class variables", a class variable is one that are associated with class instead of class object, on the other hand instance variables are variables that are associted with class object means each time a class object is intialized this object will have its own copy of that "Instance Variable" (non static) while static variable is shared among all objects of classes in running program like size of linked list etc. readonly is c# keyword that is used to make a variable readonly, java doesnt provide such facility you have to write a public method to access variables you dont wanna get tempered.