| 本帖最后由 cdinten 于 2015-9-28 09:02 编辑 
 你回答的没错,但是在C#的语法中,using有三种用法:cooolseee 发表于 2015-9-24 21:42  我看有些教程说锁文档的方法,在处理完成时,调用文档锁的Dispose()方法即可解锁
1. using指令,使用命名空间,就像一个程序开头的using System;这样的
 2. using别名,使用命名空间还可以起一个名字,例如using aClass = NameSpace1.MyToolsSets.MyClass;
 3. using语句块,定义一个范围,在范围结束时自动处理对象。例如:
 
  using (Class1 cls1 = new Class1(), cls2 = new Class1())
{
  // cls1,cls2 can be used here.
} now cls1 and cls2 has been dispose automaticly.
//here you should never use cls1,cls2
但是需要明确的是,这种语法形势下, using括号中的对象,必须是实现了IDisposable接口的对象
 所以,你的问题有两种解决方法:
 1. 使用括号形式,后面就不再显式调用Dispose方法,编译器已经为你处理是此项工作。微软使用using语句块,就是方便开发人员在写了很长一段代码之后忘记释放资源。
 2. 不使用using 语句,改用:
 
  DocumentLock dl=doc.LockDocument();
//code goes here
dl.Dispose();
这样应该说清楚了吧?
 |