C# private, static, and readonly

丶灬走出姿态 提交于 2019-11-30 03:02:14
  • 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.

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.

jpdedmon

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!