Creating an object without a link

I just have a few questions in my head ...

  • Is it good to create an object without a link, as shown below. If so, why?

    LoadIt(new myClass()); where LoadIt is some method

  • What happens to the object created above. Will it be garbage collection. If yes, then when? those. its scope is the same as other objects.

  • Is it possible to transfer the same object again?

+3
source share
6 answers

The area is determined by the method (here LoadIt) ...

Load it myClass , , , ... .. , ...

, Stack... ... LoadIt ... , , Load it method ... LoadIt ... , ( ) , ...

, , Load it ... , , ... , , .

+3

2) , ( ) , .

3) . , LoadIt.

+2

LoadIt

public MyClass LoadIt(MyClass myClass)
{
    ... do somthing
    return myClass;
}

.

+2
  • , ( # 3) MyClass .

  • , var a = new MyClass(). GCed , ; , GC .

  • . , , LoadIt .

+1

, # 2, , . , . , , - , ( , 5 GC-, , , CPU).

+1

1) , , , , . , .

2) Any managed code is garbage collection, so yes, it will be garbage collection, and when the CLR is determined.

3) yes, you can refer to it in the LoadIt method, but not outside of LoadIt.

0
source

Source: https://habr.com/ru/post/1733304/


All Articles