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;

~~~~~~

相关推荐
龙虾PRO7 分钟前
2026 DeepSeek Harness 部署完整教程:npx 一键启动至 Python SDK 全流程接入
开发语言·python
我是苏苏10 分钟前
C#基础:不写for循环的五种方式
数据结构·算法·c#
小新讲网安2 小时前
WiFi安全攻防实战:WPA3新协议与传统破解技术全解析
开发语言·网络·安全·php·漏洞·nmap·漏洞检测
必须会一定会8 小时前
Agent Plugins 1.0实战:plugin.json、skills、mcp.json目录结构与迁移
开发语言·人工智能·ai编程
St_rive8 小时前
Page Object设计模式
java·开发语言·设计模式
wp123_19 小时前
硬件元器件笔记|IPX8 防水 Type‑C 母座安费诺 124018802112A 与 TONEVEE TY48086‑24A 分析
c语言·开发语言·笔记
wuyk5559 小时前
4.树:一对多的层次数据结构
开发语言·数据结构·stm32·单片机
luj_176810 小时前
桥牌思维启示:系统设计的模块化架构
c语言·开发语言·c++·经验分享·算法
caimouse11 小时前
ReactOS 图形系统分析(16):字符串对象 — STROBJ(string.c)
c语言·开发语言