Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. Im wondering how this worked:
C#
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");
        }
    }
}


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
Posted
Updated 9-Jul-17 11:48am
Comments
PIEBALDconsult 9-Jul-17 17:09pm    
Nope. Kick is still in scope.

'kick' is declared in the main method's block and that includes any nested blocks. So, your code is valid. The other way around, a declaration in an 'inner block' is not known in an 'outer block':
C#
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).
 
Share this answer
 
Comments
The_Unknown_Member 10-Jul-17 4:14am    
This is kinda confusing. How is it even possible ? For example why I cant do this:
public static void Main()
{
int i;

for (int J = 0; J < 10; J++)
{
i = 10;
}

Console.WriteLine(i);
}


Please explain me everything man. Thanks!
[no name] 10-Jul-17 5:24am    
You could do that except that the compiler 'thinks' i may be unassigned because the for loop may not run (we know better than the compiler).If you use: int i = 10; it's OK.

It's about declaration, assignment, scope and lifetime of variables...

Maybe have a look at these explanations for more clarity?:
http://www.ict.ru.ac.za/Resources/ThinkSharply/ThinkSharply/scope_and_lifetime.html
https://tutorialstown.com/csharp-variable-scopes/
The_Unknown_Member 10-Jul-17 16:40pm    
Thanks for the links! I checked them and I learned something new. But still it didn't answer my question. Since I am bad with the explainations I will show you new piece of code:

using System;

namespace ConsoleApplication2
{
class Program
{
public static void Main()
{
int i; // i variable declaration

{
i = 5; // initialization in the inner block
}
Console.WriteLine(i); // And how here I am able to call it from the outer block ? Isn't the value assigned to i available only in the inner block ?
}
}
}
[no name] 10-Jul-17 17:22pm    
The scope (where it's 'visible' and can be used) of a variable is determined by the 'space' where it's declared. The assignment does not change the original scope of the variable.

In your example the variable i is 'visible' in the entire Main() method, including any containing ('inner') blocks, because it's declared in the most outer block:

public static void Main()
{
int i; // declaration
{
i = 1; // i is also 'visible' here
{
i = 2; // i is also 'visible' here
{
i = 3; // i is also 'visible' here
}
}
}
Console.WriteLine(i); // prints 3
}

The variable i is not 'visible' in outer blocks:

public static void Main()
{
// outer block starts here
{
int i; // declaration in inner block
{
i = 1; // i is also 'visible' here
{
i = 2; // i is also 'visible' here
{
i = 3; // i is also 'visible' here
}
}
}
}
Console.WriteLine(i); // i not 'visible' in outer block: error
}
A variable goes out of scope when execution leaves the block it was defined in,
NOT the block where it was assigned to. kick was defined in the Main method and will be visible through out the entire Main method, even inner blocks. It has nothing at all to do with where you assigned kick its value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900