Reputation: 2936
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
Reputation: 98750
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
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
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