C# using的几个用途

using 关键字有三个主要用途:

1.using 语句定义一个范围,在此范围的末尾将释放对象:

cs 复制代码
string filePath = "example.txt";
string textToWrite = "Hello, this is a test message!";
// Use the using statement to ensure the StreamWriter is properly disposed of
using (StreamWriter writer = new StreamWriter(filePath))
{
    writer.WriteLine(textToWrite);
}

2.using导入在其他命名空间中定义的类型:

cs 复制代码
using System;
using System.IO;

3.using 在命名空间下,为命名空间或类型或结构体创建别名

cs 复制代码
using Level = System.String;
using MyQuery = System.Linq;

~~~~~~

相关推荐
「QT(C++)开发工程师」16 小时前
C++ 11 常用for循环
开发语言·c++
我还记得那天16 小时前
0 初识C++
开发语言·c++
FfHUCisI16 小时前
Go 编译过程全景
开发语言·后端·golang
FfHUCisI16 小时前
Golang 语法分析与 AST:Parser 与 go/ast
开发语言·后端·golang
lemon_sjdk17 小时前
ObjectProperty
java·开发语言·算法
大模型码小白17 小时前
Spring AI Tool 实现自然语言操作 MySQL 数据库详解
服务器·开发语言·数据库·人工智能·python·mysql·spring
hh95017 小时前
Agent Plan x DeepSeek Harness:Agent 供应链安全与第三方插件审计
java·开发语言·安全·agent plan·adg成都社区·adg社区
Behaviour18 小时前
Unity UI循环列表UIScrollViewContent实现
ui·unity·c#·游戏引擎
「QT(C++)开发工程师」18 小时前
C++ std::move 详解
开发语言·c++