Namespace scope in C #

Does the namespace name define scope in the class?

  • For example, I have a namespace that is called the default name by default. Suppose in Program.cs the namespace name is "MySolution".
  • in the following file interface I created the namespace name is "Interfaces"

When I go back to the class to implement this interface, it was not in scope (I couldn't find it in intellisense). So if a class has one namespace and the interface has a second namespace, then I cannot use it in my class. I thought the namespace is only used to organize classes (like last names), but I assume that this also defines scope.

+3


source to share


1 answer


"The namespace keyword is used to declare a scope containing a set of related objects. You can use a namespace to organize code elements and create globally unique types." https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx

To use a class, interface, structure, enumeration, delegate from another namespace, you will need to use the fully qualified name of the type, or declare a reference to that namespace from using Interface;

at the top of your class. IE:



using Interfaces;  //can reference namespaces here

namespace MySolution
{
    public class Program 
    {
        //or you can use the fully qualified name of the type
        public Program(Interfaces.MyInterface example) 
        {

        }
    }
}

      

+5


source







All Articles