C#学习笔记之命名空间(包括using)

一、.Net

1:.Net由.Net平台与.NetFramework构成,.NetFramework是.Net平台不可缺少的一部分,为.Net提供一个稳定的平台,实现.Net功能。

2:交互方式有:

  • C/S模式:Client/Server,客户/服务器模式,需要下载软件,比如说:QQ;
  • B/S模式:Browser/Server,浏览器/服务器模式

3:.Net框架组件:

  • 公共语言运行库:Common Language RunTime - CLR
  • Net框架类库:.Net Framework Class Library
  • 公共语言规范:Common Language Specification
  • 通用类型系统:Common Type System
  • 元数据(Metadata)和组件(Assemblies)
  • Windows窗体(Windows Forms)
  • ASP.Net和ASP.Net AJAX
  • ADO.Net
  • Windows工作流基础(Windows Workflow Foundation - WF)
  • Windows 显示基础(Windows Presentation Foundation)
  • Windows 通信基础(Windows Communication Foundation - WCF)
  • LINQ

二、程序结构

1:一个C#程序主要包括:命名空间(Namespace)、类(Class)、方法(Method)、属性(Attribute)、语句(Statements)、表达式(Expressions)、注释

实例:

using System //引用命名空间
namespace HelloWorld
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        Console.ReadKey();
    }
}

2、命名空间(namespace)

1、定义:提供一种让一组名称与其他名称分割开的方式。即在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突。

2、语法:namespace namespace_name

3、引入:namespace_name.item_name

实例:

using System
namespace FirstName
{
    class NamaClass
    {
        public void WriteLine()
        {
            Console.WriteLine("Inside FirstName");
        }
    }
}
namespace SecondName
{
    class NamaClass
    {
        public void WriteLine()
        {
            Console.WriteLine("Inside SecondName");
        }
    }
}
namespace Test
{
    class TestClass
    {
        static void Main(string[] args)
        {
            FirstName.NamaClass firstNama = new FirstName.NamaClass();
            SecondName.NamaClass secondNama = new SecondName.NamaClass();
            firstNama.WriteLine();
            secondNama.WriteLine();
            Console.ReadLine();
        }
    }
}
当以上代码编译成功后,产生结果如下:
Inside FirstName;
Inside SecondName;

using关键字作用

①.引用命名空间,在程序中引入命名空间类型

语法:using + 命名空间名称 

作用在命名空间内部:仅在命名空间namespace内起作用;作用在命名空间外部,全局作用域

实例:

namespace FirstName
{
    using System;
    class FirstNamaClass
    {
        public void WriteLine()
        {
            Console.WriteLine("Inside FirstName");
        }
    }
}

namespace SecondName
{
    class SecondNamaClass
    {
        public void WriteLine()
        {
            Console.WriteLine("Inside SecondName");
        }
    }
}
以上在进行编译时,命名空间SecondName中编译错误”Cannot resolve symbol 'Console',原因为
using System引用仅在命名空间FirstName中起作用

②.命名空间或者类型创建别名

语法:using + 别名 = 包括详细命名空间信息的具体的类型

使用该方法的原因为:同一个文件中引用不同的命名空间中包含了相同名称的类型

实例:

using System;

namespace FirstName
{
  namespace Model
  {
    class NamaClass
    {
      public void WriteLine()
      {
        Console.WriteLine("Inside FirstName");
      }
    }
  }
}

namespace SecondName
{
  namespace Model
  {
    class NamaClass
    {
      public void WriteLine()
      {
        Console.WriteLine("Inside SecondName");
      }
    }
  }
}

namespace Test
{
  using FirstName.Model;
  using SecondName.Model;
  internal class Program
  {
    static void Main(string[] args)
    {
      NamaClass firstNama = new NamaClass();
      NamaClass secondNama = new NamaClass();
      firstNama.WriteLine();
      secondNama.WriteLine();

    }

  }

}

以上编译会报错:错误 CS0104 “NamaClass”是“FirstName.Model.NamaClass”和“SecondName.Model.NamaClass”之间的不明确的引用

此时可以写成:

using System;
namespace FirstName
{
  namespace Model
  {
    class NamaClass
    {
      public void WriteLine()
      {
        Console.WriteLine("Inside FirstName");
      }
    }
  }
}

namespace SecondName
{
  namespace Model
  {
    class NamaClass
    {
      public void WriteLine()
      {
        Console.WriteLine("Inside SecondName");
      }
    }
  }
}

namespace Test
{
  using FirstNameModel = FirstName.Model.NamaClass;
   using SecondNameModel = SecondName.Model.NamaClass;
  internal class Program
  {
    static void Main(string[] args)
    {
      FirstNameModel firstNama = new FirstNameModel();
          SecondNameModel secondNama = new SecondNameModel();
      firstNama.WriteLine();
      secondNama.WriteLine();
    }
  }
}

③using语句:将实例与代码绑定,指定使用资源的对象在关闭后释放资源

规则:

a.只能用于实现了IDisposable接口的类型;

b.适用于清理单个非托管资源的情况,多个非托管资源的清理使用try-finally

c.using语句默认生成try-finally语句,在finally块中调用对象的Dispose方法清理资源,所以using语句等效于try-finally语句。

实例:

namespace Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //using语句
            using (Form1 form1 = new Form1())
            {
                form1.ShowDialog();
            }
        }
    }
}
ShowDialog()完成后,会访问Form1中Dispose()方法释放资源

④using static指令(只能应用于类型):指定无需指定类型名称即可访问其静态成员的类型

例如:

using System
namespace Test1
{
    static void Main(string[] args)
    {
        Console.WriteLine("use namespace:using System");
    }
}
//以上代码可缩减成
using static System.Console
namespace Test1
{
    static void Main(string[] args)
    {
        WriteLine("use namespace:using tatic System.Console");
    }
}

2:其他知识点

托管资源:.Net可以自动回收的资源,主要是值托管堆上分配的内存资源;

非托管资源:.Net不知道如何回收的资源,例如:文件、窗口、网络连接、数据库连接等

posted @ 2023-10-13 10:00  ycx-x  Views(22)  Comments(0Edit  收藏  举报