这个using是什么意思,和using System; 的using是一样的吗?

swordmanli 2004-07-15 04:22:02
public CustomerData GetCustomerByEmail(String emailAddress, byte [] password)
{
//
// Check preconditions
//
ApplicationAssert.CheckCondition(emailAddress != String.Empty, "Email address is required", ApplicationAssert.LineNumber);
ApplicationAssert.CheckCondition(password.Length != 0, "Password is required", ApplicationAssert.LineNumber);
//
// Get the customer dataSet
//
CustomerData dataSet;
using (DataAccess.Customers customersDataAccess = new DataAccess.Customers())
{
dataSet = customersDataAccess.LoadCustomerByEmail(emailAddress);
}
//
// Verify the customer's password
//
DataRowCollection rows = dataSet.Tables[CustomerData.CUSTOMERS_TABLE].Rows;

if ( ( rows.Count == 1 ))
{
byte [] dbPassword = (byte[])rows[0][CustomerData.PASSWORD_FIELD];

if (ComparePasswords (dbPassword, password))
return dataSet;
else
return null;
}
else
return null;
}
...全文
792 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2004-07-15
  • 打赏
  • 举报
回复
C# 程序员参考

using 语句请参见
C# 关键字 | using 指令 | 实现 Finalize 和 Dispose 以清理非托管资源
using 语句定义一个范围,在此范围的末尾将处理对象。

using (expression | type identifier = initializer) statement
其中:

expression
希望在退出 using 语句时调用 Dispose 的表达式。
type
identifier 的类型。
identifier
type 类型的名称或标识符。定义一个以上 type 类型的 identifier 是可以的。在每一个 identifier = initializer 的前边都有一个逗号。
initializer
创建对象的表达式。
statement
嵌入的语句或要执行的语句。
备注
在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose。当到达 using 语句的末尾,或者如果在语句结束之前引发异常并且控制离开语句块,都可以退出 using 语句。

实例化的对象必须实现 System.IDisposable 接口。

示例
// cs_using_statement.cs
// compile with /reference:System.Drawing.dll
using System.Drawing;
class a
{
public static void Main()
{
using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font("Arial", 10.0f))
{
// use MyFont and MyFont2
} // compiler will call Dispose on MyFont and MyFont2

Font MyFont3 = new Font("Arial", 10.0f);
using (MyFont3)
{
// use MyFont3
} // compiler will call Dispose on MyFont3

}
}
请参见
C# 关键字 | using 指令 | 实现 Finalize 和 Dispose 以清理非托管资源



--------------------------------------------------------------------------------

向 Microsoft 发送有关此主题的反馈

© Microsoft Corporation。保留所有权利。
Tony8002003 2004-07-15
  • 打赏
  • 举报
回复
using 指令有两个用途:创建命名空间的别名(using 别名)。
允许在命名空间中使用类型,例如,不必限定该命名空间中的类型使用(using 指令)。

这里的意思是:using 语句定义一个范围,在此范围的末尾将处理对象。
可以不用导入命名空间。
也可以这么写:using System.……。它们是同等的。
jackie615 2004-07-15
  • 打赏
  • 举报
回复
guying999
Dugu_Niu 2004-07-15
  • 打赏
  • 举报
回复
在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose
以回收系统资源。
一般情况下.NET的GC会自动回收垃圾,但你并不知道它什么时候回收。
使用using 可以保证在using 语句结束时隐式调用Dispose以回收资源
jackie615 2004-07-15
  • 打赏
  • 举报
回复
using declaration
davidsoon 2004-07-15
  • 打赏
  • 举报
回复
是确保在调用完成后,调用dispose()
guying999 2004-07-15
  • 打赏
  • 举报
回复
using 语句被翻译成三个部分:获取、使用和处置。资源的使用部分被隐式封闭在一个含有 finally 子句的 try 语句中。此 finally 子句用于处置资源。如果所获取资源是 null,则不会对 Dispose 进行调用,也不会引发任何异常。

62,074

社区成员

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

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

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

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