C#中的connection.Dispose()是什么意思? [英] What is meant by connection.Dispose() in C#?
问题描述
C#中的 connection.Dispose()
是什么意思?
What is meant by connection.Dispose()
in C#?
推荐答案
A class implements IDisposable interface contains a method called Dispose()
, where you can release resources or do something else.
此外, using
语句可以帮助自动调用 Dispose()
方法.
Also, a using
statement can help to call Dispose()
method automatically.
using (SqlConnection connection = new SqlConnection(connStr))
{
//do something
}// it will automatically Dispose() here
调用 myClass.Dispose()
时会发生什么情况取决于您在 Dispose
方法中编写的内容.例如:
What happens when you call myClass.Dispose()
depends on what you wrote in the Dispose
method. For example:
public class MyClass : IDisposable
{
//since MyClass implements IDisposable, it must contain a Dispose() method otherwise will compile error
public void Dispose()
{
// do something
}
}
因此,如果您想知道调用 connection.Dispose()
时发生了什么,则必须查看连接类的 Dispose()
方法(也许是SqlConnection?).如果它是.NET内置库(这意味着您无法轻松获取源代码),则可以使用称为
so if you want to know what happened when you call connection.Dispose()
, you must take a look at the Dispose()
method of the class of connection(maybe it's a SqlConnection?). If it's a .NET built-in library(which means you can't get the source code easily), you can use a tool to help called Reflector
这篇关于C#中的connection.Dispose()是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!