What is the .NET object life cycle?

前端 未结 9 2106
一个人的身影
一个人的身影 2020-12-29 09:05

What is the object life cycle for an object in .NET?

From what I understand it is:

  1. Object created - constructor called (if one exists)
  2. Methods
9条回答
  •  梦毁少年i
    2020-12-29 09:32

    The Object Life Cycle

    Creating an object: You use the new keyword to instantiate the new object.

    1. A block of memory is allocated. This block of memory is big enough to hold the object. (CLR handles the allocation of memory for managed objects)
    2. The block of memory is converted to an object. The object is initialized. (You can control this step by implementing a constructor)

    Destroying an Object: You use destruction to reclaim any resources used by that object.

    1. The object is cleaned up; for example, by releasing any unmanaged resources used by the application, such as file handles and database connections. (You can control this step by implementing a destructor.)
    2. The memory used by the object is reclaimed.

    The CLR handles the release of memory used by managed objects; however, if you use unmanaged objects, you may need to manually release the memory used by these items.

提交回复
热议问题