C# 개체 삭제

王林
풀어 주다: 2024-09-03 15:04:30
원래의
1054명이 탐색했습니다.

데이터베이스, 파일 등에 대한 연결과 같이 관리되지 않는 리소스를 해제 및 재설정하고 메모리 정리를 수행하기 위해 C#에서 dispose of() 함수라는 함수를 사용합니다. IDisposable 인터페이스를 구현해야 하며 가비지 수집기가 아닌 사용자가 명시적으로 호출해야 합니다. dispose() 메서드는 메모리를 즉시 정리하여 메모리를 최적화하므로 파일 및 데이터베이스에 대한 연결과 같은 관리되지 않는 리소스를 정리하기 위해 이 함수를 사용하기 때문에 dispose of() 메서드를 사용하는 동안 성능과 관련된 비용은 null입니다. 많이 추천합니다.

구문:

dispose();
로그인 후 복사

dispose() 함수 작동

dispose() 함수의 작동 원리는 다음과 같습니다.

  • 데이터베이스, 파일 등에 대한 연결 등 관리되지 않는 리소스를 해제 및 재설정하고 메모리를 정리하기 위해 C#에서는 dispose of() 함수라는 함수를 사용합니다.
  • C#의 dispose() 함수는 IDisposable 인터페이스를 구현해야 합니다.
  • C#의 dispose of() 함수는 가비지 수집기가 아닌 사용자가 명시적으로 호출해야 합니다.
  • dispose of () 메서드를 사용하는 동안 성능과 관련된 비용은 null입니다. 왜냐하면 dispose of () 메서드는 메모리를 즉시 정리하여 메모리를 최적화하고 따라서 이 함수를 사용하여 파일 및 연결과 같은 관리되지 않는 리소스를 정리하기 때문입니다. 데이터베이스를 적극 권장합니다.

C# 객체 처리 예시

C# Object Dispose의 예를 살펴보겠습니다.

예시 #1

IDisposable 인터페이스를 구현하고 적절한 메시지를 표시하는 dispose() 함수를 명시적으로 호출하여 dispose() 함수의 사용법을 보여주는 C# 프로그램:

코드:

using System;
using System.IO;
using System;
//a class called demonstratingclass is defined and it implements the IDidposable interface
public class demonstratingclass : IDisposable
{
//the dispose() method is defined to to perform the release of the required resources
public void Dispose()
{
Console.WriteLine("The dispose() function has been called and the resources have been released!");
}
}
//another class called demofordispose is defined
public class demofordispose
{
//main method is called
public static void Main()
{
//an instance of the demonstratingclass is created
var result = new demonstratingclass();
//the dispose() method is called on the instance of the demonstratingclass to release it from the memory
result.Dispose();
}
}
로그인 후 복사

출력:

C# 개체 삭제

위 프로그램에서는 Demonstrating 클래스라는 클래스가 정의되어 있으며 IDidposable 인터페이스를 구현합니다. 그런 다음 필요한 리소스의 해제를 수행하기 위해 IDisposable 인터페이스를 구현하는 데모 클래스 내에 dispose of() 메서드가 정의됩니다. 그런 다음 기본 메서드가 호출되는 Demofordispose라는 또 다른 클래스가 정의됩니다. 기본 메소드 내에서 시연 클래스의 인스턴스가 생성됩니다. 그런 다음 시연 클래스의 인스턴스에서 dispose of() 함수가 호출되어 메모리에서 해제되고 해당 메시지가 표시됩니다.

예시 #2

IDisposable 인터페이스를 구현하고 적절한 메시지를 표시하는 dispose of () 함수를 명시적으로 호출하여 dispose of () 함수의 사용법을 보여주는 C# 프로그램:

코드:

using System;
//a class called demonstratingclass is defined and it implements the IDidposable interface
public class demonstratingclass : IDisposable
{
//the dispose() method is defined to to perform the release of the required resources
public void Dispose()
{
Console.WriteLine( "The resources have been released because the dispose function is called upon them");
}
}
//another class called demofordispose is defined
public class demofordispose
{
//main method is called
public static void Main()
{
//an instance of the demonstratingclass is created
var check = new demonstratingclass();
//the dispose() method is called on the instance of the demonstratingclass to release it from the memory
check.Dispose();
}
}
로그인 후 복사

출력:

C# 개체 삭제

위 프로그램에서는 Demonstrating 클래스라는 클래스가 정의되어 있으며 IDidposable 인터페이스를 구현합니다. 그런 다음 필요한 리소스의 해제를 수행하기 위해 IDisposable 인터페이스를 구현하는 데모 클래스 내에 dispose of() 메서드가 정의됩니다. 그런 다음 기본 메서드가 호출되는 Demofordispose라는 또 다른 클래스가 정의됩니다. 기본 메소드 내에서 Demonstratingclass의 인스턴스가 생성됩니다. 그런 다음 Demonstrating 클래스의 인스턴스에서 dispose of() 함수가 호출되어 메모리에서 해제되고 해당 메시지가 표시됩니다.

예시 3

IDisposable 인터페이스를 구현하고 적절한 메시지를 표시하는 dispose() 함수를 명시적으로 호출하여 dispose() 함수의 사용법을 보여주는 C# 프로그램:

코드:

using System;
//a class called demonstratingclass is defined and it implements the IDidposable interface
public class demonstratingclass : IDisposable
{
//the dispose() method is defined to to perform the release of the required resources
public void Dispose()
{
Console.WriteLine("The dispose() function is releasing the specified resources freeing them from the memory.");
}
}
//another class called demofordispose is defined
public class demofordispose
{
//main method is called
public static void Main()
{
//an instance of the demonstratingclass is created
var check1 = new demonstratingclass();
//the dispose() method is called on the instance of the demonstratingclass to release it from the memory
check1.Dispose();
}
}
로그인 후 복사

출력:

C# 개체 삭제

위 프로그램에서는 Demonstrating 클래스라는 클래스가 정의되어 있으며 IDidposable 인터페이스를 구현합니다. 그런 다음 필요한 리소스의 해제를 수행하기 위해 IDisposable 인터페이스를 구현하는 데모 클래스 내에 dispose of() 메서드가 정의됩니다. 그런 다음 기본 메서드가 호출되는 Demofordispose라는 또 다른 클래스가 정의됩니다. 기본 메소드 내에서 시연 클래스의 인스턴스가 생성됩니다. 그런 다음 시연 클래스의 인스턴스에서 dispose() 함수가 호출되어 메모리에서 해제되고 해당 메시지가 표시됩니다.

결론

이 글에서는 프로그래밍 예제와 그 출력을 통해 C#에서 () 함수의 정의, 구문, 작업을 통해 C#에서 () 함수의 개념을 배웠습니다.

위 내용은 C# 개체 삭제의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!