本文介绍了C#私有,静态的,只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在审查log4net的一些代码,我碰到这个就来了。

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.

所以,我在想,是谁写的这个代码的人。宣布它作为私人他们不希望它使用了类和静态外,这样不希望它使用的文​​件外。但是,如果有一个get属性将静电防止这种形式发生。

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.
  • 这篇关于C#私有,静态的,只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:34