using System; using System.IO; class Program { static void Main(string[] args) { StreamReader sr = new StreamReader("file.txt"); try { string line = sr.ReadLine(); Console.WriteLine(line); } finally { sr.Dispose(); } } }
using System.Data.SqlClient; class Program { static void Main(string[] args) { SqlConnection conn = new SqlConnection("connection string"); try { conn.Open(); // do some work } finally { conn.Dispose(); } } }In this example, we create a SqlConnection object to connect to a database. We call the Dispose() method in a finally block to close the connection and release any unmanaged resources used by the object. These examples use the System namespace, which is part of the .NET framework. There is no specific package library associated with the Dispose() method.