5

直接从命名空间类库而不是using命名空间显式调用方法是否有任何性能优势?

这是我所指的情况的示例:

// using
using System.Configuration;
public class MyClass
{
    private readonly static string DBConn = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;
}

对比

//explicit
public class MyClass
{
    private readonly static string DBConn = System.Configuration.ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;
}
4

3 回答 3

5

不,没有。

编译器会将所有调用转换为一个类以使用完全限定名称。

这很容易在生成的 IL 中看到,使用任何反编译器。

于 2013-04-06T19:43:12.050 回答
3

不。

编译器将为两个代码生成相同的 IL(中间语言)。因此,在这一点上不存在性能问题。

例如;

Console.WriteLine("Sample Code");

生成;

ldstr       "Sample Code"
call        System.Console.WriteLine

System.Console.WriteLine("Sample Code");

生成;

ldstr       "Sample Code"
call        System.Console.WriteLine

tl博士;编译器将这两个代码都转换为 完全限定的类名

于 2013-04-06T19:45:22.280 回答
1

这与运行时性能无关,因为它仅对编译器有意义。
编译器必须解析名称才能创建代码。这对运行时没有影响。

于 2013-04-06T19:43:46.023 回答