zoukankan
html css js c++ java
C#中using关键字的使用
C#中可使用using关键字自动清除对象资源。
# MyObject.cs
using
System;
namespace
MyProjects
{
public
class
MyObject : IDisposable
{
public
MyObject()
{ }
public
void
Dispose ( )
{
//
Dispose
Console.WriteLine (
"
Disposed
"
) ;
//
}
}
}
# Class1.cs
using
System;
namespace
MyProjects
{
public
class
Class1
{
public
Class1()
{ }
public
static
void
Main (
string
[] args )
{
using
( MyObject myObject
=
new
MyObject ( ) )
{
Console.WriteLine (
"
quit
"
) ;
}
}
}
}
使用using会自动调用MyObject的Dispose方法.
如果MyObject未实现IDispose接口,则编译时会出错:无法将类型“MyProjects. MyObject”隐式转换为“System.IDisposable”
这种写法可替代
try
{
//
}
catch
{ }
finnaly
{ MyObject.Dispose ( ) ; }
查看全文
相关阅读:
VC++技术内幕(三)
DataTable的Select方法
<转载>电话号码正则表达式
<转载>运行命令(CMD)大全
各种CSS bug与技巧
网页设计标准尺寸
CSS实用技巧及常见问题
超级实用且不花哨的js代码
广告JS代码效果大全
js小技巧收集
原文地址:https://www.cnblogs.com/ghx88/p/404977.html
最新文章
[Ubuntu] how to use urlrewrite in apache2
[html] open the link in a new page without target="_blank" (Required by w3c strict)
[Ubuntu] 将mysql的数据库转移到ntfs分区下面
[Yii Framework] Add a link in CGridView
[Ubuntu] 如何解压有乱码的zip文件
[magento] Could not determine temp directory, please specify a cache_dir manually
[水池] 灌水专用:搜狗输入法里收集的纯文字表情
[Drupal] Drupal7 How to theme a table with pager.
[Drupal] duplicated entry for meta httpequiv="ContentType" in the headarea
[HTML] how to redirect to another page automactilly without js
热门文章
[PHP] 本地服务器正常,FTP上传到服务器后页面出现乱码
[Ubuntu] 转载:Ubuntu下安装apache+php+mysql+phpmyadmin
VC中加载自己的ICO图标
.net中常用的几种页面间传递参数的方法
VC++技术内幕(四_1)
C++动态数组
Win 2003系统传真功能的配置及使用
VC++技术内幕(五_1)
VC++技术内幕(二)
VC++技术内幕(一)
Copyright © 2011-2022 走看看