EzDevInfo.com

namespaces interview questions

Top namespaces frequently asked interview questions

Unnamed/anonymous namespaces vs. static functions

A little-used feature of C++ is the ability to create unnamed (anonymous) namespaces, like so:

namespace {
    int cannotAccessOutsideThisFile() { ... }
} // namespace

You would think that such a feature would be useless -- since you can't specify the name of the namespace, it's impossible to access anything within it from outside. But these unnamed namespaces are accessible within the file they're created in, as if you had an implicit using-clause to them.

My question is, why or when would this be preferable to using static functions? Or are they essentially two ways of doing the exact same thing?


Source: (StackOverflow)

Get type name without full namespace in C#

I have the following code:

return "[Inserted new " + typeof(T).ToString() + "]";

But

 typeof(T).ToString()

returns the full name including namespace

Is there anyway to just get the class name (without any namespace qualifiers?)


Source: (StackOverflow)

Advertisements

What is the meaning of prepended double colon "::"?

I found this line of a code in a class which I have to modify:

::Configuration * tmpCo = m_configurationDB;//pointer to current db

and I don't know what exactly means the double colon prepended to the class name. Without that I would read: declaration of tmpCo as a pointer to an object of the class Configuration... but the prepended double colon confuses me.

I also found:

typedef ::config::set ConfigSet;

Source: (StackOverflow)

"Could not load type [Namespace].Global" causing me grief

In my .Net 2.0 Asp.net WebForms app, I have my Global.asax containing the following code:

<%@ Application CodeBehind="Global.asax.cs" Inherits="MyNamespace.Global" Language="C#" %>

However when I build I get an error stating-

Could not load type 'MyNamespace.Global'.

This seems to be because the MyNamespace namespace (defined in the code behind file Global.asax.cs) is not seen by the compiler in the Global.asax file (does not show in R# intellisence..). This turned out to be a very hard nut to crack... any help will be appreciated!

Note: The Global.asax and the Global.asax.cs are located in the same folder.

Note2: When compiling from the vs prompt with csc it compiles o.k.


Source: (StackOverflow)

Should 'using' statements be inside or outside the namespace?

I have been running StyleCop over some C# code, and it keeps reporting that my using statements should be inside the namespace.

Is there a technical reason for putting the using statements inside instead of outside the namespace?


Source: (StackOverflow)

What are inline namespaces for?

C++11 allows inline namespaces, all members of which are also automatically in the enclosing namespace. I cannot think of any useful application of this -- can somebody please give a brief, succinct example of a situation where an inline namespace is needed and where it is the most idiomatic solution?

(Also, it is not clear to me what happens when a namespace is declared inline in one but not all declarations, which may live in different files. Isn't this begging for trouble?)


Source: (StackOverflow)

Can someone explain __all__ in Python?

I have been using Python more and more, and I keep seeing the variable __all__ set in different __init__.py files. Can someone explain what this does?


Source: (StackOverflow)

Namespace + functions versus static methods on a class

Let's say I have, or am going to write, a set of related functions. Let's say they're math-related. Organizationally, should I:

  1. Write these functions and put them in my MyMath namespace and refer to them via MyMath::XYZ()
  2. Create a class called MyMath and make these methods static and refer to the similarly MyMath::XYZ()

Why would I choose one over the other as a means of organizing my software?


Source: (StackOverflow)

Adding System.Web.Script reference in class library

I am currently moving code from my app_code folder to a class library. I have tagged several methods with [System.Web.Script.Serialization.ScriptIgnore] attributes. My class library cannot see this namespace. My add references dialog cannot see this namespace. How do I properly use this tag from a class library?

Here is the error: The type or namespace name 'ScriptIgnoreAttribute' could not be found (are you missing a using directive or an assembly reference?)


Source: (StackOverflow)

What are namespaces?

What are PHP Namespaces?

What are Namespaces in general?

A Layman answer with an example would be great.


Source: (StackOverflow)

Why are unnamed namespaces used and what are their benefits?

I just joined a new C++ software project and I'm trying to understand the design. The project makes frequent use of unnamed namespaces. For example, something like this may occur in a class definition file:

// newusertype.cc
namespace {
  const int SIZE_OF_ARRAY_X;
  const int SIZE_OF_ARRAY_Y;
  bool getState(userType*,otherUserType*);
}

newusertype::newusertype(...) {...

What are the design considerations that might cause one to use an unnamed namespace? What are the advantages and disadvantages?


Source: (StackOverflow)

In C++, what is a "namespace alias"?

What is a "namespace alias" in C++? How is it used?


Source: (StackOverflow)

Getting "type or namespace name could not be found" but everything seems ok?

I'm getting a:

type or namespace name could not be found

error for a C# WPF app in VS2010. This area of code was compiling fine, but suddenly I'm getting this error. I've tried removing the Project Reference and the using statement, shutting VS2010 and restarting, but still I have this issue.

Any ideas why this might be occurring, where it seems like I'm doing the write thing re Reference & using statement?

I also noted in VS2010 that intellisense for that namespace is working ok, so it seems like VS2010 has the project reference and seeing the namespace on one hand, but during compile doesn't see it?


Source: (StackOverflow)

Django: "projects" vs "apps"

I have a fairly complex "product" I'm getting ready to build using Django. I'm going to avoid using the terms "project" and "application" in this context, because I'm not clear on their specific meaning in Django.

Projects can have many apps. Apps can be shared among many projects. Fine.

I'm not reinventing the blog or forum - I don't see any portion of my product being reusable in any context. Intuitively, I would call this one "application." Do I then do all my work in a single "app" folder?

If so... in terms of Django's project.app namespace, my inclination is to use myproduct.myproduct, but of course this isn't allowed (but the application I'm building is my project, and my project is an application!). I'm therefore lead to believe that perhaps I'm supposed to approach Django by building one app per "significant" model, but I don't know where to draw the boundaries in my schema to separate it into apps - I have a lot of models with relatively complex relationships.

I'm hoping there's a common solution to this...


Source: (StackOverflow)

What is the best way to solve an Objective-C namespace collision?

Objective-C has no namespaces; it's much like C, everything is within one global namespace. Common practice is to prefix classes with initials, e.g. if you are working at IBM, you could prefix them with "IBM"; if you work for Microsoft, you could use "MS"; and so on. Sometimes the initials refer to the project, e.g. Adium prefixes classes with "AI" (as there is no company behind it of that you could take the initials). Apple prefixes classes with NS and says this prefix is reserved for Apple only.

So far so well. But appending 2 to 4 letters to a class name in front is a very, very limited namespace. E.g. MS or AI could have an entirely different meanings (AI could be Artificial Intelligence for example) and some other developer might decide to use them and create an equally named class. Bang, namespace collision.

Okay, if this is a collision between one of your own classes and one of an external framework you are using, you can easily change the naming of your class, no big deal. But what if you use two external frameworks, both frameworks that you don't have the source to and that you can't change? Your application links with both of them and you get name conflicts. How would you go about solving these? What is the best way to work around them in such a way that you can still use both classes?

In C you can work around these by not linking directly to the library, instead you load the library at runtime, using dlopen(), then find the symbol you are looking for using dlsym() and assign it to a global symbol (that you can name any way you like) and then access it through this global symbol. E.g. if you have a conflict because some C library has a function named open(), you could define a variable named myOpen and have it point to the open() function of the library, thus when you want to use the system open(), you just use open() and when you want to use the other one, you access it via the myOpen identifier.

Is something similar possible in Objective-C and if not, is there any other clever, tricky solution you can use resolve namespace conflicts? Any ideas?


Update:

Just to clarify this: answers that suggest how to avoid namespace collisions in advance or how to create a better namespace are certainly welcome; however, I will not accept them as the answer since they don't solve my problem. I have two libraries and their class names collide. I can't change them; I don't have the source of either one. The collision is already there and tips on how it could have been avoided in advance won't help anymore. I can forward them to the developers of these frameworks and hope they choose a better namespace in the future, but for the time being I'm searching a solution to work with the frameworks right now within a single application. Any solutions to make this possible?


Source: (StackOverflow)