Tim Ashton
Tim Ashton

Reputation: 446

Determine the namespace of a C# file

How can I determine the namespace of a C# class if it is not defined in the file.

For example, from a class like this:

public partial class MyClass : System.Something
{


    protected void Page_Load(object sender, EventArgs e)
    {
        //do stuff

    }
        ....
}

The class is part of a project in visual studio 2012.

Upvotes: 0

Views: 217

Answers (3)

Nagaraj S
Nagaraj S

Reputation: 13484

Whether or not you explicitly declare a namespace in a C# source file, the compiler adds a default namespace. This unnamed namespace, sometimes referred to as the global namespace, is present in every file. Any identifier in the global namespace is available for use in a named namespace.

Upvotes: 1

AlexH
AlexH

Reputation: 2700

If you can edit this code, then it's preferable to define a namespace. Without any namespace, you class will be defined at the root level, just next to System / Microsoft etc... Follow the Microsoft guidelines :

As with other naming guidelines, the goal when naming namespaces is creating sufficient clarity for the programmer using the framework to immediately know what the content of the namespace is likely to be. The following template specifies the general rule for naming namespaces:

< Company>.(< Product>|< Technology>)[.< Feature>][.< Subnamespace>]

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500725

If no namespace is declared in the file, the source is in the "global" namespace. There's no namespace defaulting in C# as there is in VB.

All "real" code really should be in a namespace though, for sanity. (I write toy, throwaway code for Stack Overflow questions and blog posts in the global namespace, but nothing I'd want to keep.)

Upvotes: 1

Related Questions