用户kokos通过使用关键字回答了c#的隐藏特性问题。你能详细说明一下吗?使用的用途是什么?
当前回答
可以通过以下示例使用别名命名空间:
using LegacyEntities = CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects;
正如你所看到的,这被称为使用别名指令,它可以用来隐藏冗长的引用,如果你想在你的代码中明显地引用什么 如。
LegacyEntities.Account
而不是
CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects.Account
或者简单地
Account // It is not obvious this is a legacy entity
其他回答
大括号之外的所有内容都将被处理,因此如果您不使用对象,则可以对它们进行处理。这是因为如果你有一个SqlDataAdapter对象,你在应用程序生命周期中只使用它一次,你只填充一个数据集,你不再需要它,你可以使用以下代码:
using(SqlDataAdapter adapter_object = new SqlDataAdapter(sql_command_parameter))
{
// do stuff
} // here adapter_object is disposed automatically
另一个对象立即被处置的合理使用的例子:
using (IDataReader myReader = DataFunctions.ExecuteReader(CommandType.Text, sql.ToString(), dp.Parameters, myConnectionString))
{
while (myReader.Read())
{
MyObject theObject = new MyObject();
theObject.PublicProperty = myReader.GetString(0);
myCollection.Add(theObject);
}
}
使用using语句的原因是为了确保对象一旦超出作用域就会被释放,并且不需要显式代码来确保发生这种情况。
正如在理解c#中的“using”语句(codeproject)和使用实现IDisposable的对象(microsoft)中,c#编译器转换
using (MyResource myRes = new MyResource())
{
myRes.DoSomething();
}
to
{ // Limits scope of myRes
MyResource myRes= new MyResource();
try
{
myRes.DoSomething();
}
finally
{
// Check for a null resource.
if (myRes != null)
// Call the object's Dispose method.
((IDisposable)myRes).Dispose();
}
}
c# 8引入了一个新的语法,命名为“using declarations”:
using声明是一个前面有using关键字的变量声明。它告诉编译器,所声明的变量应该在封闭作用域的末尾被处理。
所以上面的等效代码是:
using var myRes = new MyResource();
myRes.DoSomething();
当控制离开包含范围(通常是一个方法,但也可以是一个代码块)时,myRes将被释放。
在c#中using关键字有以下两种用法。
As a directive Generally we use the using keyword to add namespaces in code-behind and class files. Then it makes available all the classes, interfaces and abstract classes and their methods and properties in the current page. Example: using System.IO; As a statement This is another way to use the using keyword in C#. It plays a vital role in improving performance in garbage collection. The using statement ensures that Dispose() is called even if an exception occurs when you are creating objects and calling methods, properties and so on. Dispose() is a method that is present in the IDisposable interface that helps to implement custom garbage collection. In other words if I am doing some database operation (Insert, Update, Delete) but somehow an exception occurs then here the using statement closes the connection automatically. No need to call the connection Close() method explicitly. Another important factor is that it helps in Connection Pooling. Connection Pooling in .NET helps to eliminate the closing of a database connection multiple times. It sends the connection object to a pool for future use (next database call). The next time a database connection is called from your application the connection pool fetches the objects available in the pool. So it helps to improve the performance of the application. So when we use the using statement the controller sends the object to the connection pool automatically, there is no need to call the Close() and Dispose() methods explicitly. You can do the same as what the using statement is doing by using try-catch block and call the Dispose() inside the finally block explicitly. But the using statement does the calls automatically to make the code cleaner and more elegant. Within the using block, the object is read-only and cannot be modified or reassigned. Example: string connString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"; using (SqlConnection conn = new SqlConnection(connString)) { SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT CustomerId, CompanyName FROM Customers"; conn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) Console.WriteLine("{0}\t{1}", dr.GetString(0), dr.GetString(1)); } }
在前面的代码中,我没有关闭任何连接;它会自动关闭。由于using语句(using (SqlConnection conn = new SqlConnection(connString)), using语句将自动调用conn. close(),对于SqlDataReader对象也是如此。并且如果发生任何异常,它将自动关闭连接。
有关更多信息,请参见在c#中使用的用法和重要性。
using语句告诉. net一旦不再需要using块中指定的对象,就释放它。
所以你应该使用'using'块来处理那些需要清理的类,比如System。IO类型。