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,
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!
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.
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.
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).
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.
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)
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.
Sorry, I know this has already been answered and it's really old, but I wanted to let anyone who comes across this article know that this is how you set up a "Singleton" pattern. Anyone who wants to know more about the code example in the question will likely benefit from learning more about Singletons and how they are used (mediators, loggers, async callbacks, etc.).
// mothership stuff about singletons
http://msdn.microsoft.com/en-us/library/ff650316.aspx
http://msdn.microsoft.com/en-us/library/ff650849.aspx
// a great SO discussion about them
What is so bad about singletons?
protected static readonly
and that earned me a "CA2104: Do not declare read only mutable reference types" warning from VS Code Analysis. As mentioned here, "Aprotected readonly
field isn't really that clear. Asprotected
, you might expect that a derived class can initialize the field." Changing the declaration toprivate static readonly
fixed this. - CrazyPyro