zoukankan      html  css  js  c++  java
  • 命名空间

    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.

  • 相关阅读:
    《软件架构师的12项修炼》阅读笔记01
    《架构即未来》阅读笔记03
    《一线架构师时间指南》-Refined Architecture阶段
    《架构即未来》阅读笔记02
    《架构即未来》阅读笔记01
    大三下第四周总结
    RPA自动化
    在shell中使用Flask
    用蓝图实现模块化应用
    请求上下文
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9717962.html
Copyright © 2011-2022 走看看