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 小时前
反射(原创推荐)
java·开发语言
yongche_shi4 小时前
ragas官方文档中文版(五十)
开发语言·python·ai·ragas·如何评估和改进 rag 应用
一路向北he4 小时前
字节钢铁军团--“提供情境,而非控制”
java·开发语言·前端
AI行业学习6 小时前
Notepad++ 官方下载 + 完整安装 + 全套优化配置(2026最新)
开发语言·人工智能·python·前端框架·html·notepad++
大圣编程7 小时前
Python中continue语句的用法是什么?
开发语言·前端·python
upgrador7 小时前
基础知识:C++ STL构造函数的左闭右开惯例及其实现原理
开发语言·c++
yoothey8 小时前
报废审批流规则引擎设计——责任链模式完整实现
linux·开发语言·bash
geovindu8 小时前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
wuyk5558 小时前
24. C 语言模块化:不是拆几个.c 文件那么简单
c语言·开发语言·stm32·单片机
北域码匠9 小时前
嵌入式限幅滤波:工业信号降噪利器
c#·传感器采集·数据预处理·嵌入式算法·限幅滤波·数字滤波·数据降噪