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;

~~~~~~

相关推荐
小灰灰搞电子4 小时前
Rust+Slint 实现动态消息提示框源码分享
开发语言·后端·rust
传奇开心果编程6 小时前
【Rust入门知识点学与练】第21课:Trait 进阶 Advanced Traits
开发语言·学习·rust
新时代牛马6 小时前
字符设备驱动完整篇:从 cdev_add、file_operations 到chrdev_open 与排障
开发语言·python
swordbob6 小时前
ReentrantLock 与 AQS 完整学习手册
java·开发语言
淡海水7 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
白山编程大哥7 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python
shmily麻瓜小菜鸡8 小时前
JavaScript / TypeScript 易踩坑知识点 —— 异步编程类
开发语言·javascript·typescript
七夜zippoe8 小时前
为什么 2026 年每个 Java 团队都该懂 AI Agent
java·开发语言·人工智能
znnnk8 小时前
【Python】GUI 开发从入门到实战(三):PyQt/PySide 进阶之路
开发语言·python·pyqt
Despacito10068 小时前
Java后端性能探查工具速查表
java·开发语言