本文介绍了[C#]在块内部制作对象的实例也在块之外工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我想知道这是如何工作的:

Hi. Im wondering how this worked:

using System;

namespace ConsoleApplication2
{
    class Program
    {
        public static void Main()
        {
            A kick = null;

            {
                kick = new A();
            } // End of the block 

            kick.HelloFromA(); // How I am able still to access it ? 
        }
    }

    class A
    {
        public void HelloFromA()
        {
            Console.WriteLine("Hello from A");
        }
    }
}





垃圾收集器不应该破坏A对象我的A型参考变量指向?



我尝试过的事情:



在CodeProject.com中提出问题



Shouldn't the gerbage collector destroy the A object to which my reference variable of type A is pointing to ?

What I have tried:

Asking question here in CodeProject.com

推荐答案

public static void Main()
{
    {
        A kick = null;
        kick = new A();
    }
 
    kick.HelloFromA(); // Here you can't access kick...
}



垃圾收集器在超出范围后清除踢,但你不知道何时(很可能不是立即)。


The garbage collector cleans up kick after it goes 'out of scope' but you just don't know when (most likely not immediately).



这篇关于[C#]在块内部制作对象的实例也在块之外工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:30