Skip to main content
 首页 » 编程设计

c#之什么时候创建变量

2024年02月13日10wuhuacong

我想知道我在下面指定的变量“temp”何时创建?

public class tempClass 
{ 
    public string prop_temp 
    { 
        get 
        { 
            string temp = "temp"; 
            return temp;//When  is the variable created ? 
        } 
    }   
} 
 
static void Main() 
{ 
    tempClass x = new tempClass(); //Is the variable created now?; 
    string b = x.prop_temp;//Or now ? 
} 

请您参考如下方法:

在这种情况下,变量是在您访问属性时创建的,而不是在您创建类时创建的。

属性 getter 实际上是方法的“简写”。因此,您的 temp 变量就像方法中的任何其他局部变量一样,在方法运行之前不存在。