• 命名空间


    Namespaces - C# Programming Guide | Microsoft Docs https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/

    Namespaces are heavily used in C# programming in two ways. First, the .NET Framework uses namespaces to organize its many classes, as follows:

    C#
    System.Console.WriteLine("Hello World!");
    

    System is a namespace and Console is a class in that namespace. The using keyword can be used so that the complete name is not required, as in the following example:

    C#
    using System;
    
    C#
    Console.WriteLine("Hello");
    Console.WriteLine("World!");
    

    For more information, see the using Directive.

    Second, declaring your own namespaces can help you control the scope of class and method names in larger programming projects. Use the namespace keyword to declare a namespace, as in the following example:

    C#
    namespace SampleNamespace
    {
        class SampleClass
        {
            public void SampleMethod()
            {
                System.Console.WriteLine(
                  "SampleMethod inside SampleNamespace");
            }
        }
    }
    

    The name of the namespace must be a valid C# identifier name.

    Namespaces Overview

    Namespaces have the following properties:

    • They organize large code projects.
    • They are delimited by using the . operator.
    • The using directive obviates the requirement to specify the name of the namespace for every class.
    • The global namespace is the "root" namespace: global::System will always refer to the .NET System namespace.

    using directive - C# Reference | Microsoft Docs https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive

    The using directive has three uses:

    • To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:

      C#
      using System.Text;
      
    • To allow you to access static members and nested types of a type without having to qualify the access with the type name.

      C#
      using static System.Math;
      

      For more information, see the using static directive.

    • To create an alias for a namespace or a type. This is called a using alias directive.

      C#
      using Project = PC.MyCompany.Project;
      

    The using keyword is also used to create using statements, which help ensure that IDisposable objects such as files and fonts are handled correctly. See using Statement for more information.

    Using static type

    You can access static members of a type without having to qualify the access with the type name:

    C#
    using static System.Console;
    using static System.Math;
    class Program
    {
        static void Main()
        {
            WriteLine(Sqrt(3*3 + 4*4));
        }
    }
    

    Remarks

    The scope of a using directive is limited to the file in which it appears.

    The using directive can appear:

    • At the beginning of a source code file, before any namespace or type definitions.
    • In any namespace, but before any namespace or types declared in this namespace.

    Otherwise, compiler error CS1529 is generated.

    Create a using alias directive to make it easier to qualify an identifier to a namespace or type. In any using directive, the fully-qualified namespace or type must be used regardless of the using directives that come before it. No using alias can be used in the declaration of a using directive. For example, the following generates a compiler error:

    C#
    using s = System.Text;
    using s.RegularExpressions;
    

    Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.

    Namespaces come in two categories: user-defined and system-defined. User-defined namespaces are namespaces defined in your code. For a list of the system-defined namespaces, see .NET API Browser.

    For examples on referencing methods in other assemblies, see Create and Use Assemblies Using the Command Line.

  • 相关阅读:
    2019春第十一周作业
    2019春第十周作业
    2019春第九周作业
    2019春第八周作业
    2019春第七周作业
    2019春第六周作业
    寒假作业一:打印沙漏
    寒假作业三:抓老鼠啊亏了~还是赚了?
    寒假作业二:币值转换
    秋季学期学习总结
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9717962.html
Copyright © 2020-2023  润新知