我有以下简单的类定义(Windows控制台应用程序):
样品按预期进行
using System;
namespace ConsoleApplication1 {
class Test: IDisposable {
string Text;
public Test(string str) {
Text = str;
}
public void print() {
Console.WriteLine("Text: " + Text);
}
public void Dispose() {
Text = "";
}
}
class Program {
static void Main(string[] args) {
string TeXT = "Data Source=localhost;Initial Catalog=master;";
using (Test test = new Test(TeXT)) {
test.print();
}
using (Test test = new Test(TeXT)) {
test.print();
}
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我传递string给一个IDisposable类 …