[& Basic theory] C # namespace (namespace)

Namespace: all names defined in .NET applications, including variable names, are included in the namespace. Using namespace hierarchy, we usually need to qualify the name based on the name of the name space to access them.


E.g:

Suppose we create a console application project called the MyConsoleApp

Program files in the project are as follows:

namespace MyConsoleApp
{
    internal class Program
    {
        private static void Main()
        {
        }
    }
}

The project shows that the top-level namespace: namespace MyConsoleApp, and code files or methods or classes in all of the projects included in the MyConsoleApp namespace, suppose we create a public class named Common.cs, reads as follows :

namespace MyConsoleApp
{
    internal class Common
    {
public static int GetDefaultAge() { return 80; } } }

Thus, Common.cs included in the namespace MyConsoleApp, including under the age of such a static method of obtaining default, if you want to refer to it in the main method Program.cs file, then you can directly use the following references:

namespace MyConsoleApp 
{ 
    Internal  class Program 
    { 
        Private  static  void the Main () 
        { 
            // complete REFERENCE 
            var defaultAge = MyConsoleApp.Common.GetDefaultAge ();
             // because the two files in the same namespace, and then it is not necessary to specify MyConsoleApp 
            var = defaultAge2 Common.GetDefaultAge (); 
        } 
    } 
}

Guess you like

Origin www.cnblogs.com/gme5/p/11797947.html