C #에서 클래스의 소멸자와 Finalize 메서드의 차이점은 무엇입니까?
클래스에서 소멸자와 Finalize 메서드의 차이점은 무엇입니까?
최근에 Visual Studio 2008이 Finalize 메서드와 동의어 인 소멸자를 고려한다는 사실을 발견했습니다. 즉, Visual Studio에서는 클래스에서 두 메서드를 동시에 정의 할 수 없습니다.
예를 들어, 다음 코드 조각 :
class TestFinalize
{
~TestFinalize()
{
Finalize();
}
public bool Finalize()
{
return true;
}
}
소멸자에서 Finalize를 호출 할 때 다음 오류를 제공합니다.
다음 메서드 또는 속성간에 호출이 모호합니다. 'TestFinalize. ~ TestFinalize ()'및 'TestFinalize.Finalize ()'
Finalize에 대한 호출이 주석 처리되면 다음 오류가 발생합니다.
'ManagementConcepts.Service.TestFinalize'유형은 이미 동일한 매개 변수 유형으로 'Finalize'라는 멤버를 정의합니다.
C #의 소멸자는 System.Object.Finalize
메서드를 재정의 합니다. 그렇게 하려면 소멸자 구문 을 사용해야합니다. 수동으로 재정의 Finalize
하면 오류 메시지가 표시됩니다.
기본적으로 Finalize
메서드 선언 으로하려는 것은 기본 클래스의 메서드를 숨기는 것입니다. 컴파일러가 new
수정자를 사용하여 음소거 할 수있는 경고를 발행하게합니다 (작동 할 경우). 여기서 주목해야 할 중요한 점은 동일한 이름을 가진 멤버를 동시에 선언 할 수 없다는 것입니다. 따라서 소멸자와 메서드 를 모두 사용 하면 오류가 발생합니다 ( 권장되지 는 않지만 다음과 같은 경우 메서드를 선언 할 수 있음). 소멸자를 선언하지 않습니다).override
new
Finalize
public new void Finalize()
위키 백과는 종결 자와의 차이에 대한 좋은 논의가 소멸자 에서 종료 자 기사.
C #에는 실제로 "진정한"소멸자가 없습니다. 구문은 C ++ 소멸자와 비슷하지만 실제로는 종료 자입니다. 예제의 첫 번째 부분에서 올바르게 작성했습니다.
~ClassName() { }
위는 Finalize
함수에 대한 구문 설탕입니다 . 기본의 종료자가 실행되도록 보장하지만 그렇지 않으면 Finalize
함수 재정의와 동일 합니다. 즉, 소멸자 구문을 작성할 때 실제로 종료자를 작성하는 것입니다.
Microsoft에 따르면 종료 자는 가비지 수집기가 수집 ( Finalize
) 할 때 호출하는 함수를 참조하는 반면 소멸자는 결과로 실행되는 코드 비트 (가되는 구문 설탕 Finalize
)입니다. 그들은 마이크로 소프트가 결코 구별을하지 말았어야하는 똑같은 것에 매우 가깝습니다.
Microsoft에서 C ++의 "소멸자"용어를 사용하는 것은 잘못된 것입니다. C ++에서는 개체가 삭제되거나 스택에서 제거되는 즉시 동일한 스레드에서 실행되는 반면 C #에서는 다른 시간에 별도의 스레드에서 실행되기 때문입니다.
파괴 장치
They are special methods that contains clean up code for the object. You can not call them explicitly in your code as they are called implicitly by GC. In C# they have same name as the class name preceded by the
~
sign. Like-Class MyClass { ~MyClass() { ..... } }
In VB.NET, destructors are implemented by overriding the Finalize method of the System.Object class.
Dispose
These are just like any other methods in the class and can be called explicitly but they have a special purpose of cleaning up the object. In the dispose method we write clean up code for the object. It is important that we freed up all the unmanaged recources in the dispose method like database connection, files etc. The class implementing dispose method should implement IDisposable interface.A Dispose method should call the GC.SuppressFinalize method for the object it is disposing if the class has desturctor because it has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method. Reference: http://msdn2.microsoft.com/en-us/library/aa720161(VS.71).aspx
Finalize
A Finalize method acts as a safeguard to clean up resources in the event that your Dispose method is not called. You should only implement a Finalize method to clean up unmanaged resources. You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically. Finalize method is called by the GC implicitly therefore you can not call it from your code.
Note: In C#, Finalize method can not be override, so you have to use destructor whose internal implementation will override the Finalize method in MSIL.But in the VB.NET, Finalize method can be override because it does support destructor method.
Update: Interesting semi-related thread here.
'IT story' 카테고리의 다른 글
Golang에서 코드 커버리지를 측정하는 방법은 무엇입니까? (0) | 2020.09.07 |
---|---|
React의 JSX 구문에서 이중 중괄호의 목적은 무엇입니까? (0) | 2020.09.07 |
기본 키 / 외래 키 명명 규칙 [닫힘] (0) | 2020.09.07 |
while 루프 내에서 bash에서 입력 읽기 (0) | 2020.09.07 |
CORS-httprequest를 어떻게 '프리 플라이트'합니까? (0) | 2020.09.07 |