BenR
BenR

Reputation: 2936

Is there a performance difference between using a namespace vs explicitly calling the class from the namespace?

Is there any performance benefit to explicitly calling a method directly from a namespace class library rather than using the namespace?

Here is an example of a situation I'm referring to:

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

vs.

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

Upvotes: 5

Views: 460

Answers (3)

Soner Gönül
Soner Gönül

Reputation: 98750

No.

Compiler will generates the same IL (Intermediate Language) for both codes. So, there is no performance issue on this point.

For example;

Console.WriteLine("Sample Code");

generates;

ldstr       "Sample Code"
call        System.Console.WriteLine

and

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

generates;

ldstr       "Sample Code"
call        System.Console.WriteLine

tl dr; the compilers converts both code to fully qualified class names.

Upvotes: 3

bash.d
bash.d

Reputation: 13207

This has nothing to do with the runtime-performance, as it is solely of significance for the compiler.
The compiler must resolve the names in order to create code. This his no impact on run-time.

Upvotes: 1

Oded
Oded

Reputation: 499002

No, there isn't any.

The compiler will convert all calls to a class to use the fully qualified name anyway.

This is easy enough to see in the produced IL, using any decompiler.

Upvotes: 5

Related Questions