简体   繁体   中英

C# private, static, and readonly

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,

  • 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!

静态并不意味着它不能从其他文件被访问-这不是C. static关键字意味着记录器对象是类变量,而不是一个实例变量,因此,即使从该类的不同的对象被访问时,他们将all都指向同一个logger对象。

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.

开发人员所说的是,当他们在这个类的任何实例中调用logger.Info(...)时,他们想要使用一个公共(静态)实例(所以不需要为每个类实例创建一个新的记录器),他们希望确定它自创建以来没有改变(只读),如果我们在派生类中处于虚函数中,那么我想确保我不会错误地使用基类(私有)。

静态变量属于“类变量”的类别,类变量是与类而不是类对象关联的变量,另一方面,实例变量是与类对象关联的变量意味着每次类对象初始化此对象时不会有自己的“实例变量”(非静态)副本,而静态变量在运行程序中的所有类对象之间共享,如链表等大小.readonly是c#关键字,用于生成变量readonly,java不提供这样的设施你必须编写一个公共方法来访问你不想得到缓和的变量。

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)

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM