Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

performance

c#

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;
}
like image 281
BenR Avatar asked Apr 06 '13 19:04

BenR


People also ask

What is the biggest advantage of using namespaces?

The biggest advantage of using namespace is that the class names which are declared in one namespace will not clash with the same class names declared in another namespace. It is also referred as named group of classes having common features.

What is the difference between a namespace and a class?

Classes are data types. They are an expanded concept of structures, they can contain data members, but they can also contain functions as members whereas a namespace is simply an abstract way of grouping items together. A namespace cannot be created as an object; think of it more as a naming convention.

What is the advantage of using namespace in C++?

Namespace in C++ is the declarative part where the scope of identifiers like functions, the name of types, classes, variables, etc., are declared. The code generally has multiple libraries, and the namespace helps in avoiding the ambiguity that may occur when two identifiers have the same name.

Why is using namespace std considered a bad practice?

While this practice is okay for example code, pulling in the entire std namespace into the global namespace is not good as it defeats the purpose of namespaces and can lead to name collisions. This situation is called namespace pollution.


1 Answers

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.

like image 131
Oded Avatar answered Sep 24 '22 20:09

Oded