Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What namespace will a class have if no namespace is defined

Tags:

namespaces

c#

In C#, if I create a class with no namespace, what namespace will I use when trying to instantiate the class?

For example, assume main is...

namespace NamespaceTests {     class Program     {         static void Main(string[] args)         {          }     } } 

... and assume my namespace-less class is ...

public class test {     public string SayHello()     {         return "Hello World!";     } } 

... and assume I have another class by the same name, but having the default namespace...

namespace NamespaceTests {     public class test     {         public string SayHello()         {             return "Hello Moon...";         }     } } 

... how would I modify main to include an instance of the namespace-less class and call 'SayHello' to retrieve the message "Hello World!"? Specifically, how would I fully qualify the namespace-less instance of class 'test', especially considering I may have another class also called 'test' but having a namespace, so I need to distinguish...

like image 675
barrypicker Avatar asked Aug 25 '14 17:08

barrypicker


People also ask

Can we have a class without namespace?

Yes we can create class without namespace.

What is the namespace of 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.

Do you have to have a namespace in C#?

There is no need to have a namespace. However developer studio expects you to be using a name space. For example, when you choose to add a class to a project developer studio will: Create a file for the class.


1 Answers

It's in the global namespace and can be referenced like this:

var x = new global::test();

like image 166
David Crowell Avatar answered Nov 07 '22 03:11

David Crowell