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...
Yes we can create class without namespace.
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.
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.
It's in the global namespace and can be referenced like this:
var x = new global::test();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With