问题描述
请考虑此code:
1)public static class MyClass
2){
3) public static DateTime MyMethod(DateTime dt)
4) {
5) DateTime temp = new DateTime();
6) temp = dt.AddDays(1);
7) return temp;
8) }
9)}
确实温度
变量例如每任何调用的MyMethod
?或者是因为它是在内部静态类温度
变量只有一个实例的静态方法分配内存?
Does temp
variable has instance per any calls to MyMethod
? or because it is in a static method inside static class just one instance of temp
variable allocate in memory?
感谢
推荐答案
温度
既不是静态的,也不是一个实例变量,它是一个的本地变量。它完全没有关系的声明它的方法是静态的还是不:变量的作用域开始在其声明的位置,并结束于右花括号}
的范围中声明它。每一个执行线程经过的MyMethod
都有自己的温度
,拷贝是不可见的变量的范围之内的任何地方。
temp
is neither a static nor an instance variable, it is a local variable. It absolutely does not matter whether the method in which it is declared is static or not: the variable's scope starts at the point of its declaration, and ends at the closing curly brace }
of the scope in which it is declared. Each executing thread that goes through MyMethod
gets its own copy of temp
, which is invisible anywhere outside the variable's scope.
这篇关于在内部静态类的静态方法变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!