为了获得最佳网站性能,使用:(例如)
Telerik.Web.UI.RadChart ResultChart = new Telerik.Web.UI.RadChart();
比:
using Telerik.Web.UI;
和
RadChart ResultChart = new RadChart();
如果我using
在 ASP.net 页面的许多模块中使用了该指令,编译器会使用一次吗?
为了获得最佳网站性能,使用:(例如)
Telerik.Web.UI.RadChart ResultChart = new Telerik.Web.UI.RadChart();
比:
using Telerik.Web.UI;
和
RadChart ResultChart = new RadChart();
如果我using
在 ASP.net 页面的许多模块中使用了该指令,编译器会使用一次吗?
没有什么不同的。每次需要使用该类型时,编译后的 IL 将包含完全限定名称。
该using
语句只是使您的代码文件更短且更易于阅读。
指令在您的代码中出现的次数也没有什么区别——只要项目引用它所在的程序集就足够了。
没有区别。
using
语句使您的代码更短且更具可读性。如果在多个模块中使用它是没有问题的。为这两个版本创建了相同的 IL(中间语言)代码。您可以查看这两条代码及其IL代码;
using System;
namespace Programs
{
public class Program
{
public static void Main(string[] args)
{
string s = "Foo";
}
}
}
.method public hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 8 (0x8)
.maxstack 1
.locals init ([0] string s)
IL_0000: nop
IL_0001: ldstr "Foo"
IL_0006: stloc.0
IL_0007: ret
} // end of method Program::Main
namespace Programs
{
public class Program
{
public static void Main(string[] args)
{
System.String s = "Foo";
}
}
}
.method public hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 8 (0x8)
.maxstack 1
.locals init ([0] string s)
IL_0000: nop
IL_0001: ldstr "Foo"
IL_0006: stloc.0
IL_0007: ret
} // end of method Program::Main