C# オブジェクトの破棄

王林
リリース: 2024-09-03 15:04:30
オリジナル
1054 人が閲覧しました

データベースやファイルなどへの接続など、管理されていないリソースを解放してリセットし、メモリのクリーンアップを実行するには、C# の destroy of () 関数と呼ばれる関数を使用します。 IDisposable インターフェイスを実装する必要があり、ガベージ コレクターではなくユーザーが明示的に呼び出す必要があります。 destroy() メソッドを使用する際のパフォーマンスに関するコストは null です。これは、dispose() メソッドがメモリを即座にクリーンアップしてメモリを最適化し、そのため、この関数を使用してファイルやデータベースへの接続などのアンマネージド リソースをクリーンアップするのは非常にコストがかかるためです。とてもおすすめです。

構文:

dispose();
ログイン後にコピー

dispose() 関数の動作

dispose() 関数の動作は次のとおりです。

  • データベースやファイルなどへの接続など、管理されていないリソースを解放してリセットし、メモリのクリーンアップを実行するには、C# の destroy of () 関数と呼ばれる関数を利用します。
  • C# の destroy() 関数は IDisposable インターフェイスを実装する必要があります。
  • C# の destroy of () 関数は、ガベージ コレクターではなくユーザーが明示的に呼び出す必要があります。
  • dispose of () メソッドを使用する際のパフォーマンスに関するコストは null です。dispose of () メソッドはすぐにメモリをクリーンアップしてメモリを最適化し、この関数を使用してファイルや接続などのアンマネージド リソースをクリーンアップするためです。データベースは非常にお勧めです。

C# オブジェクトの破棄の例

C# オブジェクトの破棄の例について説明します。

例 #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# オブジェクトの破棄

上記のプログラムでは、デモンストレーションクラスというクラスが定義されており、IDidposableインターフェースを実装しています。次に、必要なリソースの解放を実行する IDisposable インターフェイスを実装するデモンストレーション クラス内で、dispose of () メソッドが定義されます。次に、demofordispose という別のクラスが定義され、その中で main メソッドが呼び出されます。 main メソッド内で、デモンストレーション クラスのインスタンスが作成されます。次に、デモンストレーション クラスのインスタンスで destroy of () 関数が呼び出され、メモリからインスタンスが解放され、適切なメッセージが表示されます。

例 #2

IDisposable インターフェイスを実装し、適切なメッセージを表示する destroy 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# オブジェクトの破棄

上記のプログラムでは、デモンストレーションクラスというクラスが定義されており、IDidposableインターフェースを実装しています。次に、必要なリソースの解放を実行する IDisposable インターフェイスを実装するデモンストレーション クラス内で、dispose of () メソッドが定義されます。次に、demofordispose という別のクラスが定義され、その中で main メソッドが呼び出されます。メイン メソッド内で、デモンストレーション クラスのインスタンスが作成されます。次に、デモンストレーション クラスのインスタンスで destroy 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# オブジェクトの破棄

上記のプログラムでは、デモンストレーションクラスというクラスが定義されており、IDidposableインターフェースを実装しています。次に、必要なリソースの解放を実行する IDisposable インターフェイスを実装するデモンストレーション クラス内で、dispose of () メソッドが定義されます。次に、demofordispose という別のクラスが定義され、その中で main メソッドが呼び出されます。 main メソッド内で、デモンストレーション クラスのインスタンスが作成されます。次に、デモンストレーション クラスのインスタンス上で destroy() 関数が呼び出され、メモリからインスタンスが解放され、適切なメッセージが表示されます。

結論

この記事では、プログラミング例とその出力を通じて、C# の destroy of () 関数の定義、構文、動作を通じて、C# の destroy of () 関数の概念を学びました。

以上がC# オブジェクトの破棄の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!