What is the .NET object life cycle?

前端 未结 9 2130
一个人的身影
一个人的身影 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条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 09:13

    Here are the steps I know of:

    1. load the assembly
    2. execute static initialisers
    3. "new" call:
      1. allocate memory
      2. execute non-static initialisers
      3. execute constructor
    4. the instance is now ready to be used
    5. after the last reference to the object has vanished: if the object has no finalizer, it is now ready for collection; if the object has a finalizer, it is put on the finalizer queue.
    6. (optional) the objects from the finalizer queue have their finalizer called in a special thread; if there is still no reference from the application to the object, it too becomes now eligible for garbage collection
    7. the garbage collector deallocates memory

    As others already have pointed out, Dispose() must be called by the user since the runtime won't act on it.

提交回复
热议问题