Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What code is allowed outside namespace except using directives

Using directives outside namespace body is common practice, but what are other code which can be done outside namespace body.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The namespace is optional. If it is omitted, types will be assigned to the global namespace. Therefore, every kind of code allowed at the top-level is allowed outside of the namespace.

// No namespace declaration here
public class MyClass()
{
}

You can explicitly specify this namespace like this:

var c = new global::MyClass();

The documentation says:

The compiler adds a default namespace. This unnamed namespace, sometimes referred to as the global namespace, is present in every file. It contains declarations not included in a declared namespace. Any identifier in the global namespace is available for use in a named namespace.

See: Learn / .NET / C# guide / Language reference / Keywords / namespace

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading