先建一个 Student 类,它实现了 IDisposable 接口:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Student 的摘要说明
/// </summary>
public class Student : IDisposable
{
private String _Name;
private Int32 _Age;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="name"></param>
/// <param name="age"></param>
public Student(String name, Int32 age)
{
_Name = name;
_Age = age;
}
/// <summary>
/// 释放对象
/// </summary>
public void Dispose()
{
//
}
/// <summary>
/// 姓名
/// </summary>
public String Name
{
get { return _Name; }
set { _Name = value; }
}
/// <summary>
/// 年龄
/// </summary>
public Int32 Age
{
get { return _Age; }
set { _Age = value; }
}
}using 语句使用示例:
Student stu;
using (stu = new Student("AA", 20))
{
Response.Write(stu.Name);
}
if (stu is Student)
Response.Write("yes");
else
Response.Write("no");说明:
1.Student 必须实现 IDisposable 接口,否则程序将出错,提示:using 语句中使用的类型必须可隐式转换为“System.IDisposable”;
2.输出yes,说明调用Dispose后,对象并没有被释放;
参考文章:http://msdn.microsoft.com/zh-cn/asp.net/yh598w02.aspx
评论(0)
暂无评论。