C#中using关键字的3中用法
一、导入命名空间
using System;
二、强制释放资源
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
以上代码的等同写法:
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
三、定义别名(命名空间别名、类别名)
using System;
using System.Data.SqlClient;
using colAlias = System.Collections;
namespace System
{
using cout = System.Console;
class TestClass
{
static void
{
colAlias::Hashtable test = new colAlias::Hashtable();
cout.WriteLine("hello");
}
}
}