using引用的问题

骑着蜗牛去爬山 2012-01-25 08:31:46
using(OleDbConnection conn=new OleDbConnection())
{
;
}
上面的代码正常,不报错
using (string s = "aa")
{
;
}
以上代码报错:“string”: using 语句中使用的类型必须可隐式转换为“System.IDisposable”
请教高手,到底什么时候用using ,什么时候不用using?
如果我自定义一个类(继承IDisposable),应该怎么写?
请高手帮帮我这个菜鸟,祝所有人新年快乐。
...全文
228 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
宝_爸 2012-01-25
  • 打赏
  • 举报
回复
GC.Collect();
释放的是托管资源,而不是非托管资源。
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 findcaiyzh 的回复:]
你的实现IDisposable接口中定义的方法啊

public class a:IDisposable

{
public int aa(int i)
{
return i;
}

public void Dispose()
{
释放你的非托管资源。
}

}
[/Quote]
3Q
还有个小问题
public void Dispose()
{
GC.Collect();
}
除了这种方法还有什么方法释放非托管资源?
宝_爸 2012-01-25
  • 打赏
  • 举报
回复
你的实现IDisposable接口中定义的方法啊

public class a:IDisposable

{
public int aa(int i)
{
return i;
}

public void Dispose()
{
释放你的非托管资源。
}

}
  • 打赏
  • 举报
回复
public class a:IDisposable

{
public int aa(int i)
{
return i;
}
}
提示错误 1 “a”不会实现接口成员“System.IDisposable.Dispose()”。
为什么呢?
宝_爸 2012-01-25
  • 打赏
  • 举报
回复
提示的很清楚了,只有实现了“System.IDisposable”的类才能用到using中。

你自己的类,只要实现了“System.IDisposable”接口就可以了。


using System;
using System.ComponentModel;

// The following example demonstrates how to create
// a resource class that implements the IDisposable interface
// and the IDisposable.Dispose method.

public class DisposeExample
{
// A base class that implements IDisposable.
// By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources.
public class MyResource: IDisposable
{
// Pointer to an external unmanaged resource.
private IntPtr handle;
// Other managed resource this class uses.
private Component component = new Component();
// Track whether Dispose has been called.
private bool disposed = false;

// The class constructor.
public MyResource(IntPtr handle)
{
this.handle = handle;
}

// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
protected virtual void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// Dispose managed resources.
component.Dispose();
}

// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero;

// Note disposing has been done.
disposed = true;

}
}

// Use interop to call the method necessary
// to clean up the unmanaged resource.
[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);

// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
~MyResource()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}
}
public static void Main()
{
// Insert code here to create
// and use the MyResource object.
}
}


以上代码来自msdn:
http://msdn.microsoft.com/en-us/library/system.idisposable.dispose.aspx

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧